<form action="${pageContext.request.contextPath }/user/queryUsers.action" method="post"> <table> <tr> <td>名称:<input name="userCustom.name" /> <input type="submit" value="提交" /> </td> </tr> </form>Controller:@RequestMapping("/queryUsers") public String queryUsers(Model model, UsersQueryVo usersQueryVo) throws Exception { List<UserCustom> usersList = usersservice.findUsersList(usersQueryVo); model.addAttribute("userList", usersList); return "user/user"; }注意:一定要考虑程序的可扩展性,多用包装类,应对多个pojo数组类型绑定需求:批量删除用户实现方式:jsp多选框名称和形参名称保持一致jsp页面:<c:forEach items="${userList}" var="item"> <tr> <td><input type="checkbox" name="user_id" value="${item.id }" /></td> <td>${item.name}</td> </tr></c:forEach>Controller:@RequestMapping("/deleteUsers") public String deleteUsers(Integer[] user_id) throws Exception { return "success"; }List集合类型绑定需求:批量修改用户信息实现方式:1.查询包装类里添加集合属性,用于修改的信息传递到Controller2.前端页面注意是按照集合索引的形式赋值jsp页面:<form name="form1" action="${pageContext.request.contextPath }/user/editAllUsersSubmit.action" method="post"> <table> <tr> <td colspan="4"><input type="submit" value="批量修改" /></td> </tr> <tr> <td>name</td> <td>age</td> <td>address</td> <td>modifytime</td> </tr> <c:forEach items="${userList}" var="item" varStatus="status"> <tr> <td><input name="userCustoms[${status.index}].name" value="${item.name}" /></td> <td><input name="userCustoms[${status.index}].age" value="${item.age}" /></td> <td><input name="userCustoms[${status.index}.address" value="${item.address}" /></td> <td><input name="userCustoms[${status.index}].modifytime" value='<fmt:formatDate value="${item.modifytime }" pattern="yyyy-MM-dd HH:mm:ss" />' /></td> </tr> </c:forEach> </table> </form>Controller:@RequestMapping("/editAllUsers") public String editAllUsers(Model model) throws Exception { List<UserCustom> usersList = usersService.findUsersList(null); model.addAttribute("userList", usersList); return "user/editAllUsers"; } @RequestMapping("/editAllUsersSubmit") public String editAllUsersSubmit(UsersQueryVo usersQueryVo) throws Exception { return "success"; }pojo包装对象:public class UsersQueryVo { PRivate List<UserCustom> userCustoms; public List<UserCustom> getUserCustoms() { return userCustoms; } public void setUserCustoms(List<UserCustom> userCustoms) { this.userCustoms = userCustoms; }}Map类型绑定在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。jsp页面:<tr><td>信息:</td><td>姓名:<inputtype="text"name="itemInfo['name']"/>年龄:<inputtype="text"name="itemInfo['age']"/>.. .. ..</td></tr>Contrller:public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{System.out.println(queryVo.getStudentinfo());}包装类中定义Map对象如下:Public class QueryVo {private Map<String, Object> itemInfo = new HashMap<String, Object>(); //get/set方法..}
新闻热点
疑难解答