04-struts2获得参数

1 struts2 获得参数 1-属性驱动获得参数

1 Demo8Action

package www.test.c_param;

import java.util.Date;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


//struts2获得参数的方式1-属性驱动获得参数
public class Demo8Action extends ActionSupport {

    
    //每次请求Action时都会创建新的Action实例对象
    public Demo8Action() {
        super();
        System.out.println("demo8Action被创建了!");
    }
    
    public String execute() throws Exception {
        ActionContext actionContext = ActionContext.getContext();
        actionContext.put("username", username);
        actionContext.put("age", age);
        actionContext.put("birthday", birthday);
        return SUCCESS;
    }
    
    //准备与参数键名称相同的属性
    private String username;
    
    //自动类型转换 只能转换8大基本数据类型以及对应包装类
    private Integer age;
    
    //支持特定类型字符串转换为Date ,例如 yyyy-MM-dd
    private Date birthday;
    
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

}

2 struts.xml

<action name="Demo8Action" class="www.test.c_param.Demo8Action" method="execute">
      <result name="success" type="dispatcher">/regist.jsp</result>
</action>

3 form1.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>
    <form action="${pageContext.request.contextPath }/Demo8Action" method="post">
        用户名:<input type="text" name="username"><br/>
        年龄:<input type="text" name="age"><br/>
        生日:<input type="text" name="birthday"><br/>
        <input type="submit" value="注册">
    </form>
</body>
</html>

4 regist.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>
    用户名:${requestScope.username }<br/>
    年龄:${requestScope.age }<br/>
    出生日期:${requestScope.birthday }<br/>
</body>
</html>

2 struts2 获得参数 2-对象驱动

1 User类

package www.test.domain;

import java.util.Date;

public class User {

    private String username;
    private Integer age;
    private Date birthday;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", age=" + age + ", birthday=" + birthday + "]";
    }
    
}
View Code

2 Demo9Action

package www.test.c_param;

import com.opensymphony.xwork2.ActionSupport;

import www.test.domain.User;

//struts2获得参数的方式1-属性驱动获得参数
public class Demo9Action extends ActionSupport {

    //准备user对象
    private User user;
    
    public String execute() throws Exception {
        System.out.println(user);
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    
    
}

3 struts.xml

<action name="Demo9Action" class="www.test.c_param.Demo9Action" method="execute">
    <result name="success" type="dispatcher">/form2.jsp</result>
</action>

4 form2.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>
    <form action="${pageContext.request.contextPath }/Demo9Action" method="post">
        用户名:<input type="text" name="user.username"><br/>
        年龄:<input type="text" name="user.age"><br/>
        生日:<input type="text" name="user.birthday"><br/>
        <input type="submit" value="注册">
    </form>
</body>
</html>

3 struts2 获得参数 3-模型驱动

1 Demo10Action

package www.test.c_param;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import www.test.domain.User;

//struts2 获得参数 3-模型驱动
public class Demo10Action extends ActionSupport implements ModelDriven<User> {

    // User对象
    private User user = new User();
    
    
    public String execute() throws Exception {
        ActionContext context = ActionContext.getContext();
        context.put("username", user.getUsername());
        context.put("age", user.getAge());
        context.put("username", user.getBirthday());
        return SUCCESS;
    }


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

}

2 struts.xml

<action name="Demo10Action" class="www.test.c_param.Demo10Action" method="execute">
    <result name="success" type="dispatcher">/regist.jsp</result>
</action>

3 form3.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>struts2获得参数方式3-模型驱动</title>
</head>
<body>
    <form action="${pageContext.request.contextPath }/Demo8Action" method="post">
        用户名:<input type="text" name="username"><br/>
        年龄:<input type="text" name="age"><br/>
        生日:<input type="text" name="birthday"><br/>
        <input type="submit" value="注册">
    </form>
</body>
</html>

4 struts2 获得参数方式-集合类型封装

1 Demo11Action

package www.test.c_param;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

//struts2 封装集合类型参数
public class Demo11Action extends ActionSupport {
    //list
    private List list;
    //map
    private Map<String,String> map;
    
    public String execute() throws Exception {
        //list.clear();
        System.out.println(list);
        System.out.println(map);
        return SUCCESS;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

2 struts.xml

<action name="Demo11Action" class="www.test.c_param.Demo11Action" method="execute">
    <result name="success" type="dispatcher">/form4.jsp</result>
</action>

3 form4.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>struts2 获得参数方式-集合类型封装</title>
</head>
<body>
    <form action="${pageContext.request.contextPath }/Demo11Action" method="post">
        list:<input type="text" name="list"><br/>
        list:<input type="text" name="list[5]"><br/>
        map:<input type="text" name="map['name']"><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

posted @ 2018-03-07 17:10  Jepson6669  阅读(160)  评论(0编辑  收藏  举报