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

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

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

  1.属性驱动获得参数

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

 public class Demo8Action extends ActionSupport{
//准备与参数键
private String name;
//自动类型转换 只能转换8大基本数据类型以及对应包装类
private Integer age;
//支持特定类型字符串转换为Date,例如yyyy-mm-dd
private Date birthday; public String getName() {
return name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public void setName(String name) {
this.name = name;
} @Override
public String execute() throws Exception {
System.out.println("name参数值:"+name+",age参数值:"+age+"生日:"+birthday);
return SUCCESS;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} }

 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种)、集合类型参数封装

上一篇:brew的MAC安装


下一篇:解决Eclipse启动报错【Failed to create the Java Virtual Machine】