Struts2-整理笔记Action生命周期如何获取参数(3种)集合类型参数封装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2-整理笔记Action生命周期如何获取参数(3种)集合类型参数封装相关的知识,希望对你有一定的参考价值。

一、Action生命周期
  每次请求到来时,都会创建一个新的Action实例
  Action是线程安全的,可以使用成员变量接收参数

二、获取参数的方式(3种)

  1.属性驱动获得参数

    每次请求Action时都会创建新的Action实例对象

 1 public class Demo8Action extends ActionSupport{
 2     //准备与参数键
 3     private String name;
 4     //自动类型转换 只能转换8大基本数据类型以及对应包装类
 5     private Integer age;
 6     //支持特定类型字符串转换为Date,例如yyyy-mm-dd
 7     private Date birthday;
 8     
 9     public String getName() {
10         return name;
11     }
12 
13     public Integer getAge() {
14         return age;
15     }
16 
17     public void setAge(Integer age) {
18         this.age = age;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     @Override
26     public String execute() throws Exception {
27         System.out.println("name参数值:"+name+",age参数值:"+age+"生日:"+birthday);
28         return SUCCESS;
29     }
30 
31     public Date getBirthday() {
32         return birthday;
33     }
34 
35     public void setBirthday(Date birthday) {
36         this.birthday = birthday;
37     }
38 
39 }

 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">
        用户名:<input type="text" name="name" /><br>
        年龄:<input type="text" name="age" /><br>
        生日:<input type="text" name="birthday" /><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

  2.对象驱动获得参数

public class Demo9Action extends ActionSupport {
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return super.execute();
    }
}

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">
        用户名:<input type="text" name="user.name" /><br>
        年龄:<input type="text" name="user.age" /><br>
        生日:<input type="text" name="user.birthday" /><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

  3.模型驱动获得参数

  第一步实现ModelDriven并规定泛型 重写方法 返回 泛型内容
  第二步准备user 成员变量
  有一个缺点 只能返回一个对象

public class Demo10Action extends ActionSupport implements ModelDriven<User>{

    private User user = new User();

    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return super.execute();
    }

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

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}/Demo10Action">
        用户名:<input type="text" name="name" /><br>
        年龄:<input type="text" name="age" /><br>
        生日:<input type="text" name="birthday" /><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

三、集合类型参数封装

public class Demo11Action extends ActionSupport {
    //list
    private List<String> list;
    //Map
    private Map<String,String> map;
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("map:"+map);
        System.out.println("list:"+list);
        return super.execute();
    }
}

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}/Demo11Action" method="post">
        list:<input type="text" name="list" /><br>
        list:<input type="text" name="list[3]" /><br>
        map:<input type="text" name="map[‘haha‘]" /><br>
        map:<input type="text" name="map[‘lala‘]" /><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

技术分享图片

 





以上是关于Struts2-整理笔记Action生命周期如何获取参数(3种)集合类型参数封装的主要内容,如果未能解决你的问题,请参考以下文章

Struts2学习笔记整理

值栈ValueStack的原理与生命周期

Struts2-整理笔记拦截器拦截器配置

浅析Struts1和Struts2的Action线程安全问题

Struts2中的值栈

请求生命周期: