2011-09-14 31 views
8

我似乎無法讓我的形式綁定到複選框控制。我在這裏讀了很多帖子,並嘗試了一些技巧,但沒有運氣。也許一個新的眼睛會有所幫助。春綁定列表<Object>形成:複選框

我的控制器:

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> 

生成的HTML:

<span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">User</label></span><span><br/><input id="roles2" name="roles" type="checkbox" value="2"/><label for="roles2">Admin</label></span><span><br/><input id="roles3" name="roles" type="checkbox" value="3"/><label for="roles3">SuperAdmin</label></span<input type="hidden" name="_roles" value="on"/> 

我使用的第二對每個環路(未示出),以確保模型對象包含角色。它確實,但沒有選中複選框,並且當我提交角色對象時總是空的。有人能告訴我我錯過了什麼嗎?

感謝

編輯

對不起剛剛意識到這可能是有益的,看看帳號和角色對象:

public class Account { 

    private String username, firstName, lastName, email; 
    private List<Role> roles; 

    @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 Role { 

private int id; 
private String name; 

public Role() {} 

public Role(int id, String name) { 
    this.id = id; 
    this.name = name; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

@NotNull 
@Size(min = 1, max = 50) 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

}

編輯#2

控制器後方法

@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); 
} 

AccountEditForm是表單支持對象。當我進行GET時,我將獲取一個Account對象,並在顯示屏幕之前將這些值傳遞給AccountEditForm。爲了清楚起見,我將附上AccountEditForm。與賬戶對象非常相似。我碰巧把我的表單對象從我的模型對象中分離出來。

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); 
    } 

} 

編輯#3角色屬性編輯器

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())); 
} 

編輯#4 NEW ProeprtyEditor

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); 
    } 
} 

回答

3

添加equals方法來你的角色實體。

看到answer (Spring MVC Pre Populate Checkboxes):瞭解更多詳情類似的問題的。

+0

感謝您的答覆。我在我的角色對象中添加了等號,現在當我查看屏幕時,複選框已正確填充,但是當我進行更改或進行提交時,綁定到表單的帳戶對象的角色屬性始終爲空。 – blong824

+0

@ blong824角色對象是否未提交,未在帳戶對象中填充或未保存的問題? – Ralph

+0

我刪除了所有要保存的代碼,並將打印語句添加到post方法中。角色對象始終爲空。所以不能在Account對象中填充。 – blong824

0

我發現問題是使用路徑複雜對象的名單。 combox似乎只提交「價值」字段中的內容。 所以我改變綁定到List而不是List的路徑的變量,並發現列表由ID填充。

0

看來你缺少實施equals並在您的實體hashCode方法。