Struts2请求参数封装四——集合参数封装
集合参数封装需要借助于struts2请求参数封装中属性封装有实体类的方式。
- 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>