Struts2--封装集合类型的数据(List、Map)

封装集合类型的数据

在实际的开发中,有些时候我们需要批量插入用户或者其他的对象,在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]}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值