怎样在struts2中完整遍历list
摸鱼童子 2017-09-21 09:20:29 public class UserAction extends ActionSupport{
private User user;
private List<User> userList;
private List<User> dbList= new ArrayList<User>();
public String saveUser() {
System.out.println(user.getUsername());
System.out.println(user.getEducationList().get(0).getSchool());
System.out.println(user.getSex());
System.out.println("--------");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
dbList.add(user);
System.out.println(dbList.size());
for(User u : dbList){
System.out.println(u.getUsername());
}
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
前台:
<body>
部门管理>>添加员工<br>
<form action="saveUserAction" method="post">
姓名:<input type="text" name="user.username"><br>
年龄:<input type="text" name="user.age"><br>
性别:<input type="text" name="user.sex"><br>
教育背景<br>
<table border="1px">
<tr>
<td>起始年份</td>
<td>结束年份</td>
<td>教育机构</td>
<td>所获学历</td>
</tr>
<tr>
<td><input type="text" name="user.educationList[0].beginYear"/></td>
<td><input type="text" name="user.educationList[0].endYear"/></td>
<td><input type="text" name="user.educationList[0].school"/></td>
<td><input type="text" name="user.educationList[0].grade"/></td>
</tr>
<tr>
<td><input type="text" name="user.educationList[1].beginYear"/></td>
<td><input type="text" name="user.educationList[1].endYear"/></td>
<td><input type="text" name="user.educationList[1].school"/></td>
<td><input type="text" name="user.educationList[1].grade"/></td>
</tr>
<tr>
<td><input type="text" name="user.educationList[2].beginYear"/></td>
<td><input type="text" name="user.educationList[2].endYear"/></td>
<td><input type="text" name="user.educationList[2].school"/></td>
<td><input type="text" name="user.educationList[2].grade"/></td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</body>
每次测试打印的结果
--------
1
tom2
为什么之前提交的数据没有存到list里面?怎么用session来解决?