我有控制器,可以添加由管理员分配给用户的计算机。用户可以拥有多个已分配的计算机,并通过Spring MVC Multiple Selection实现。但是我在更新后显示信息的问题很小。它在用户列表中显示尽可能多的用户,因为分配了许多计算机。我认为JSP和调试器中的问题没有显示,所以我非常感谢你的帮助。
这是我的 AdminUserUpdateController.java
@Controller
public class AdminUserUpdateController {
@InitBinder("userForm")
private void initBinder(WebDataBinder binder) {
binder.setValidator(updateValidator);
binder.registerCustomEditor(Set.class, "computers", new CustomCollectionEditor(Set.class) {
@Override
protected Object convertElement(Object element) {
String pcName = null;
if (element instanceof String && !((String) element).equals("")) {
try {
pcName = (String) element;
Set<Computer> computerSet = new LinkedHashSet<>();
computerSet.add(computerService.getComputerByName(pcName));
new UserForm().setComputers(computerSet);
} catch (NumberFormatException e) {
logger.info("Error in element");
logger.info("Element was " + ((String) element));
}
}
return pcName != null ? computerService.getComputerByName(pcName) : null;
}
});
}
...............
@RequestMapping(value = "/adminEdit/{userId}", method = RequestMethod.GET)
public String updateView(@PathVariable("userId") Integer userId,
UserForm userForm,
ModelMap model) {
userForm.setUser(userService.getUserById(userId));
model.addAttribute("userForm", userForm);
model.addAttribute("computers", computerService.getAllComputers());
return "adminUserUpdate";
}
@RequestMapping(value = "adminEdit.do/{userId}", method = RequestMethod.POST)
public ModelAndView updateUserProcess(@ModelAttribute(value = "userForm")
UserForm userForm,
@PathVariable("userId") Integer userId,
BindingResult result, Model model) {
User user = userService.getUserById(userId);
model.addAttribute("computers", computerService.getAllComputers());
model.addAttribute("userForm", userForm);
updateValidator.validate(userForm, result);
return updatingUser(user, model, userForm);
}
private ModelAndView updatingUser(User user, Model model,
UserForm userForm) {
if (isEmailExists(userForm, user)) {
model.addAttribute("errorMsg", "Email is already in use!");
return new ModelAndView("adminUserUpdate");
}
fillForm(userForm, user);
user = userForm.getUser();
userService.updateUser(user);
return new ModelAndView("redirect:/adminPage", "user", user);
}
private void fillForm(UserForm userForm, User user) {
userForm.setUserId(user.getUserId());
userForm.setLogin(user.getLogin());
userForm.setRegDate(user.getRegDate());
// userForm.setComputers(computer);
userForm.setRole(roleService.findByName(user.getRole().getRoleName()));
}
}
这是我的JSP
<!--BEGIN OF USERS TABLE-->
<div align="center">
<table border=1>
<thead align="center">
<tr>
<th>User Id</th>
<th>Login</th>
<th>Name</th>
<th>Password</th>
<th>Email</th>
<th>Regsiter Date</th>
<th>PC Assigned</th>
<th colspan=2>Action</th>
</tr>
</thead>
<tbody align="center">
<tr>
<c:forEach items="${members}" var="user">
<td><c:out value="${user.userId}"/></td>
<td><c:out value="${user.login}"/></td>
<td><c:out value="${user.name}"/></td>
<td><c:out value="${user.password}"/></td>
<td><c:out value="${user.email}"/></td>
<td><fmt:formatDate pattern="yyyy-MM-dd" value="${user.regDate}"/></td>
<td>
<c:choose>
<c:when test="${user.computers!= null && !user.computers['empty']}">
<c:forEach items="${user.computers}" var="comp">
<c:out value="${comp.pcName}"/>
</c:forEach>
</c:when>
<c:otherwise>
<p class="h3_error">No PC Assigned</p>
</c:otherwise>
</c:choose>
</td>
<td><a href="${app}/adminEdit/${user.userId}">Update</a></td>
<td><a href="${app}delete?userId=${user.userId}">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<br>
</div>
<!--END OF USERS TABLE-->