struts2怎样遍历list的全部数据
摸鱼童子 2017-09-21 01:08:33 action:
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";
}
前台:
<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
tom1
list里面只有一组数据,之前提交的就显示不出来。我知道是每次都new了一个新的list但是怎么用session改呢?求大神解答!