Struts2请求参数封装四

Struts2请求参数封装四——集合参数封装
集合参数封装需要借助于struts2请求参数封装中属性封装有实体类的方式。

  1. List集合
    即将表单数据提交到集合中,作为集合元素存在

案例如下:

Action:
public class HelloAction extends ActionSupport {
	private List<User> users;
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public String sayHello() {
		System.out.println(users);
		return SUCCESS;
	}
}

form表单:

<form action="${pageContext.request.contextPath}/hello" method="post">
		users:<input type="text" name="users[0].user"/><br/>
		age:<input type="text" name="users[0].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users[0].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users[0].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users[0].hobby">Coding<input/><br/>
		 	users:<input type="text" name="users[1].user"/><br/>
		age:<input type="text" name="users[1].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users[1].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users[1].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users[1].hobby">Coding<input/><br/>
				 	users:<input type="text" name="users[2].user"/><br/>
		age:<input type="text" name="users[2].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users[2].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users[2].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users[2].hobby">Coding<input/><br/>
		 <input type="submit" value="submit">
	</form>

2.Map集合封装
Action:

public class HelloAction extends ActionSupport {
	private Map<String,User> users;
	public Map<String, User> getUsers() {
		return users;
	}
	public void setUsers(Map<String, User> users) {
		this.users = users;
	}
	public String sayHello() {
		System.out.println(users);
		return SUCCESS;
	}
}

form表单:

<form action="${pageContext.request.contextPath}/hello" method="post">
		users:<input type="text" name="users['key1'].user"/><br/>
		age:<input type="text" name="users['key1'].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users['key1'].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users['key1'].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users['key1'].hobby">Coding<input/><br/>
		 	users:<input type="text" name="users['key2'].user"/><br/>
		age:<input type="text" name="users['key2'].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users['key2'].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users['key2'].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users['key2'].hobby">Coding<input/><br/>
				 	users:<input type="text" name="users['key3'].user"/><br/>
		age:<input type="text" name="users['key3'].age"><br/>
		hobby:	<input type="checkbox" value="Eating" name="users['key3'].hobby">Eating<input/>
				<input type="checkbox" value="Sleeping" name="users['key3'].hobby">Sleeping<input/>
				<input type="checkbox" value="Coding" name="users['key3'].hobby">Coding<input/><br/>
		 <input type="submit" value="submit">
	</form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值