title: Spring Binding List<Object> to Form:checkboxes
body: <p>I can't seem to get my form to bind to the checkboxes control.  I read many posts on here and tried a few techniques but no luck.  Maybe a fresh set of eyes will help.</p>
  
  <p>my controller:</p>
  
  <pre><code>public String editAccount(@RequestParam("id") String id, Model model) {
      model.addAttribute("account", accountService.getAccount(id));
      model.addAttribute("allRoles", roleService.getRoles());
      return EDIT_ACCOUNT;
  }
  </code></pre>
  
  <p>my jsp:</p>
  
  <pre><code>&lt;form:form action="" modelAttribute="account"&gt;
  &lt;form:checkboxes items="${allRoles}" path="roles" itemLabel="name" itemValue="id" delimiter="&lt;br/&gt;"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>the generated html:</p>
  
  <pre><code>&lt;span&gt;&lt;input id="roles1" name="roles" type="checkbox" value="1"/&gt;&lt;label for="roles1"&gt;User&lt;/label&gt;&lt;/span&gt;&lt;span&gt;&lt;br/&gt;&lt;input id="roles2" name="roles" type="checkbox" value="2"/&gt;&lt;label for="roles2"&gt;Admin&lt;/label&gt;&lt;/span&gt;&lt;span&gt;&lt;br/&gt;&lt;input id="roles3" name="roles" type="checkbox" value="3"/&gt;&lt;label for="roles3"&gt;SuperAdmin&lt;/label&gt;&lt;/span&lt;input type="hidden" name="_roles" value="on"/&gt;
  </code></pre>
  
  <p>I used a second for each loop (not shown) to make sure that the model object contained the roles.  It does, yet none of the checkboxes are checked and when I submit the roles object is always empty.  Can someone please tell me what I'm missing?</p>
  
  <p>Thanks</p>
  
  <p>EDIT</p>
  
  <p>Sorry just realized it might be helpful to see the account and role objects:</p>
  
  <pre><code>public class Account {
  
      private String username, firstName, lastName, email;
      private List&lt;Role&gt; 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&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(List&lt;Role&gt; 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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>EDIT #2</p>
  
  <p>Controller Post Method</p>
  
  <pre><code>@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);
  }
  </code></pre>
  
  <p>AccountEditForm is the form backing object.  When I do a GET I grab an Account object and pass the values into an AccountEditForm before displaying the screen.  I will attach the AccountEditForm just for clarity.  It is very similiar to the account object.  I just happened to seperate my form objects from my model objects.</p>
  
  <pre><code>public class AccountEditForm {
  
      private String username, firstName, lastName, email;
      private List&lt;Role&gt; roles = new ArrayList&lt;Role&gt;();
  
      @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&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(List&lt;Role&gt; roles) {
          this.roles = roles;
      }
  
      public String toString() {
          return ReflectionToStringBuilder.toString(this);
      }
  
  }
  </code></pre>
  
  <p>EDIT #3 Role Property Editor</p>
  
  <pre><code>public class RolePropertyEditor extends PropertyEditorSupport {
  
      private Map&lt;Integer, Role&gt; roleMap = new HashMap&lt;Integer, Role&gt;();
  
      public RolePropertyEditor(List&lt;Role&gt; 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());
      }
  }
  </code></pre>
  
  <p>Defined in my controller like:</p>
  
  <pre><code>@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()));
  }
  </code></pre>
  
  <p>EDIT #4 NEW ProeprtyEditor</p>
  
  <pre><code>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);
      }
  }
  </code></pre>