Java 弹簧装订清单<;对象>;要形成:复选框
我似乎无法将表单绑定到复选框控件。我在这里读了很多帖子,尝试了一些技巧,但运气不好。也许换一双眼睛会有帮助 我的控制器:Java 弹簧装订清单<;对象>;要形成:复选框,java,spring,binding,checkbox,Java,Spring,Binding,Checkbox,我似乎无法将表单绑定到复选框控件。我在这里读了很多帖子,尝试了一些技巧,但运气不好。也许换一双眼睛会有帮助 我的控制器: public String editAccount(@RequestParam("id") String id, Model model) { model.addAttribute("account", accountService.getAccount(id)); model.addAttribute("allRoles", roleService.getR
public String editAccount(@RequestParam("id") String id, Model model) {
model.addAttribute("account", accountService.getAccount(id));
model.addAttribute("allRoles", roleService.getRoles());
return EDIT_ACCOUNT;
}
我的jsp:
<form:form action="" modelAttribute="account">
<form:checkboxes items="${allRoles}" path="roles" itemLabel="name" itemValue="id" delimiter="<br/>"/>
</form>
AccountEditForm是表单支持对象。当我执行GET时,我抓取一个Account对象,并在显示屏幕之前将值传递到AccountEditForm中。为了清楚起见,我将附上AccountEditForm。它与account对象非常相似。我只是碰巧将表单对象与模型对象分开
public class AccountEditForm {
private String username, firstName, lastName, email;
private List<Role> roles = new ArrayList<Role>();
@NotNull
@Size(min = 1, max = 50)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@NotNull
@Size(min = 1, max = 50)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min = 1, max = 50)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@NotNull
@Size(min = 6, max = 50)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
编辑#4新建项目编辑器
public class SecurityRolePropertyEditor extends PropertyEditorSupport {
private RoleService roleService;
public SecurityRolePropertyEditor(RoleService roleService) {
this.roleService = roleService;
}
public void setAsText(final String name) {
Role role = roleService.getRoleByName(name);
setValue(role);
}
}
将equals方法添加到角色实体
查看此:类似问题的详细信息。您似乎缺少实体上的实现
equals
和hashCode
方法。感谢您的回复。我添加了等于我的角色对象,现在当我查看屏幕时,复选框填充正确,但当我进行更改或提交时,绑定到表单的帐户对象的角色属性始终为空。@blong824是角色对象未提交的问题,未在Account对象中填充或未保存?我删除了所有要保存的代码,只是在post方法中添加了一条打印语句。角色对象始终为空。因此未在Account对象中填充。@blong824:是否可以添加post方法?@blong824:是否有任何已注册的PropertyEdit或converter将角色的id转换为其角色对象?
@RequestMapping(value = "edit", method = RequestMethod.POST)
public String updateAccount(@RequestParam("id") String id, @ModelAttribute("account") @Valid AccountEditForm form, BindingResult result) {
System.out.println("FORM VALUES AFTER: " + form);
return (result.hasErrors() ? EDIT_ACCOUNT : ACCOUNT_REDIRECT);
}
public class AccountEditForm {
private String username, firstName, lastName, email;
private List<Role> roles = new ArrayList<Role>();
@NotNull
@Size(min = 1, max = 50)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@NotNull
@Size(min = 1, max = 50)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min = 1, max = 50)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@NotNull
@Size(min = 6, max = 50)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
public class RolePropertyEditor extends PropertyEditorSupport {
private Map<Integer, Role> roleMap = new HashMap<Integer, Role>();
public RolePropertyEditor(List<Role> roleList) {
for (Role r : roleList) roleMap.put(r.getId(), r);
}
public void setAsText(String incomingId) {
Role role = roleMap.get(incomingId);
System.out.println("PROPERTY EDITOR ROLE " + role);
setValue(role);
}
public String getAsText() {
System.out.println("PROPERTY EDITOR ID " + ((Role)getValue()).getId());
return String.valueOf(((Role)getValue()).getId());
}
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setAllowedFields(new String[] {
"username", "password", "confirmPassword", "firstName", "lastName", "email", "acceptTerms", "currentPassword"
});
binder.registerCustomEditor(Role.class, new RolePropertyEditor(roleService.getRoles()));
}
public class SecurityRolePropertyEditor extends PropertyEditorSupport {
private RoleService roleService;
public SecurityRolePropertyEditor(RoleService roleService) {
this.roleService = roleService;
}
public void setAsText(final String name) {
Role role = roleService.getRoleByName(name);
setValue(role);
}
}