0
我遇到了form:checkbox的問題。我無法讓它顯示選定的值。當我選擇值並提交時,正確的值顯示在數據庫中。當我加載頁面時,所有的值(複選框)都沒有被選中。如何綁定Spring窗體:複選框而不是窗體:複選框?
單元下位於這裏面:
<form:form role="form" commandName="user" class="form-horizontal" action="${form_url}">
</form:form>
這只是正常:
<form:checkboxes items="${availableRoles}" path="roles" itemLabel="role" itemValue="id" element="div class='checkbox'"/>
這不起作用:
<c:forEach items="${availableRoles}" var="r" varStatus="status">
<div class="checkbox">
<form:checkbox path="roles" label="${r.description}" value="${r.id}"/>
</div>
</c:forEach>
這是我的域類:
public class User {
private List<Role> roles;
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
這是我的自定義屬性編輯器:
public class RolePropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
Role role = new Role();
role.setId(Integer.valueOf(text));
setValue(role);
}
}
控制器有以下方法:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Role.class, new RolePropertyEditor());
}
控制器的方法:
@RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
public String updateUser(@PathVariable Integer userId, Model model) {
User user = userService.getByUserId(userId);
List<Role> availableRoles = roleService.getAllRoles();
model.addAttribute("availableRoles", availableRoles);
model.addAttribute("user", user);
return "user/update";
}