关键点:封装一个JavaBean的List
原始JavaBean
public class Person {
//省略getter、setter,Serialiable接口实现,toString(),equals(),HashCode()
private String username;
private String password;
private String money;
}
封装JavaBean的另一个Bean
public class PersonListModel {
private List<Person> personList;
//省略相关setter、getter
}
//处理方法
@RequestMapping(value="/listPerson.do",method=RequestMethod.GET)
public ModelAndView listPersonGET() {
//得到所有Person,封装至Model,供前台显示
List<Person> list = personService.getAllPersons();
ModelAndView mav = new ModelAndView("person/listPerson"); //设置视图名
mav.addObject("list", list);
return mav;
}
表单jsp页面,name值需要和Controller层接收参数中的JavaBean要一致,如这里的:personList。
<form action="${bastPath}/person/listPerson.do" method="post">
<c:forEach items="${list}" var="person" varStatus="listIndex" >
<br/>${person.username}<br/>
<input name="personList[${listIndex.index}].money" type="text" value="person.money"/>
</c:forEach>
<input name="submit" value="提交">
</form>
//使用参数PersonListModel personModel获取jsp页面表单批量提交的数据
@RequestMapping(value="/listPerson.do",method=RequestMethod.POST)
public ModelAndView listPersonPOST(PersonListModel personModel) {
//于是表单中的personList就会set给PersonListModel 中的字段personList
List<Person> personList = personModel.getPersonList();
for(Person person:personList ) { //批量修改对象的某个属性
personService.updatePerson(person.money);
}
}