封装集合类型的数据
在实际的开发中,有些时候我们需要批量插入用户或者其他的对象,在Action中需要接收到多个Action中封装的对象,然后传递给业务层。那么这个时候就需要将表单的数据封装到集合中。一般我们通常使用的集合无非是List或者是Map集合。
封装到List集合中
User类:
public class User {
private String name;
private Integer age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
}
}
表单页面listFrom.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}/ListFormAction" method="post">
用户名:<input type="text" name="list[0].name" /><br>
年龄:<input type="text" name="list[0].age" /><br>
用户名:<input type="text" name="list[1].name" /><br>
年龄:<input type="text" name="list[2].age" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
ListFormAction:
public class ListFormAction extends ActionSupport{
private List<User> list;
@Override
public String execute() throws Exception {
System.out.println(list);
return "success";
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
}
配置:
<action name="ListFormAction" class="pers.zhang.action.ListFormAction" method="execute" >
<result name="success" type="dispatcher" >/listForm.jsp</result>
</action>
测试:
控制台输出:
[User [name=tom, age=18, birthday=null], User [name=Jerry, age=null, birthday=null], User [name=null, age=28, birthday=null]]
封装数据到Map集合
页面mapForm.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}/MapFormAction" method="post">
用户名:<input type="text" name="map['one'].name" /><br>
年龄:<input type="text" name="map['one'].age" /><br>
用户名:<input type="text" name="map['two'].name" /><br>
年龄:<input type="text" name="map['two'].age" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
MapFormAction:
public class MapFormAction extends ActionSupport{
private Map<String, User> map;
@Override
public String execute() throws Exception {
System.out.println(map);
return "success";
}
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
}
配置:
<action name="MapFormAction" class="pers.zhang.action.MapFormAction" method="execute" >
<result name="success" type="dispatcher" >/mapForm.jsp</result>
</action>
测试:
控制台打印:
{one=User [name=tom, age=18, birthday=null], two=User [name=jerry, age=22, birthday=null]}