在不使用 json 格式的数据的时候仍然需要使用包装类:
package com.qjl.ssm.sysmanage.dto;
import java.io.Serializable;
import java.util.List;
import com.qjl.ssm.sysmanage.entity.User;
/**
* 包装类:UserDTO
*/
public class UserDTO extends User {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
User 实体类如下:
package com.qjl.ssm.sysmanage.entity;
import java.io.Serializable;
import java.util.Date;
public class User {
private Integer userId;
private String userName;
private String loginName;
private String password;
private Integer age;
private String sex;
private Integer deptId;
private Date birthday;
private Date tvUpdate;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName == null ? null : loginName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getTvUpdate() {
return tvUpdate;
}
public void setTvUpdate(Date tvUpdate) {
this.tvUpdate = tvUpdate;
}
}
前台页面的代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户列表</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<h1>批量修改</h1>
<form action="<%=path%>/sysmgr/user/batchUpdateUser" method="post">
<table>
<thead>
<tr>
<th>全选</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<c:if test="${userList != null}">
<c:forEach items="${requestScope.userList}" var="user" varStatus="i">
<tr>
<td><input type="hidden" name="userList[${i.index}].userId" value="${user.userId}" /></td>
<td><input type="text" name="userList[${i.index}].userName" value="${user.userName}" /></td>
<td><input type="text" name="userList[${i.index}].age" value="${user.age}" /></td>
<td><input type="text" name="userList[${i.index}].sex" value="${user.sex}" /></td>
</tr>
</c:forEach>
</c:if>
<c:if test="${userList == null}">
<tr>
<td colspan="4">没有查询到用户信息</td>
</tr>
</c:if>
</tbody>
</table>
<input type="submit" value="批量修改用户" />
</form>
</body>
</html>
后台 SpringMVC 的接收的代码:
@RequestMapping("/batchUpdateUser")
public String batchUpdateUser(UserDTO userDTO, Model model) {
if (userDTO.getUserList() != null && userDTO.getUserList().size() > 0) {
if (this.userService.updateUserBatch(userDTO.getUserList())) {
model.addAttribute("editMsg", "批量修改成功");
} else {
model.addAttribute("editMsg", "批量修改失败");
}
}
return "forward:/sysmgr/user/getUserList";
}