struts2入门

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,

在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。

 

环境搭配

 

.添加依赖
    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-config-browser-plugin</artifactId>
    <version>2.5.13</version>
    </dependency>

 

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.huang</groupId>
  <artifactId>T226_struts01</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>T226_struts01 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.13</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>T226_struts01</finalName>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
    </plugins>
  </build>
</project>

struts-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <package name="base" extends="struts-default" abstract="true">
        <global-allowed-methods>regex:.*</global-allowed-methods>
    </package>
</struts>

struts-sy.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="sy" extends="base" namespace="/sy">
    <action name="/hello_*" class="com.huang.one.web.HelloAction" method="{1}">
        <result name="success">/success.jsp</result>
    </action>
    </package>
</struts>

 

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <include file="struts-default.xml"></include>
    <include file="struts-base.xml"></include>
    <include file="struts-sy.xml"></include>
</struts>

1、动态方法调用(mvc不具备的优势)

HelloAction.java

package com.huang.one.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.huang.one.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;


public class HelloAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
    private HttpServletResponse response;
    private HttpServletRequest request;
    private User user1=new User();
    private User user2;
    private String sex;
    private String uname;
    
    public User getUser2() {
        return user2;
    }

    public void setUser2(User user2) {
        this.user2 = user2;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String add() {
        System.out.println("add方法");
        return "success";
    }
    
    public String del() {
        System.out.println("del方法");
        return "success";
    }
    
    public String edit() {
        System.out.println("edit方法");
        return "success";
    }
    
    public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        System.out.println("uname:"+uname);
//        HttpServletRequest request = ServletActionContext.getRequest();
//        request.setAttribute("rs", user1);
        this.request.setAttribute("rs",user1);
        return "success";
    }

    @Override
    public User getModel() {
        return user1;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
    }


}

 

2、struts中的传参
1、set传参
2、参数名,属性名传参
3、实现modeldriven接口传参

demo1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>struts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名传参</a>
<h2>与j2EE容器的交互</h2>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功页面:${user2 }
</body>
</html>

 

运行结果:

 

posted @ 2019-07-25 21:26  宇大..大  阅读(120)  评论(0编辑  收藏  举报