title: Spring MVC usage of form:checkbox to bind data
body: <p>I know there have been already questions around this topic, but I have not figured out how to solve the following issue:</p>
  
  <p>I have a user/roles relationship and I want to list all available roles in a JSP as checkbox items, where the users assigned checkboxes are selected. However, the matching items are not checked (here I am using Spring 3.1).</p>
  
  <p>Extract from the User object:</p>
  
  <pre><code>private Set&lt;RoleEntity&gt; roles = new HashSet&lt;RoleEntity&gt;();
  </code></pre>
  
  <p>Extract from the Spring Controller (adding user object and role list to Model):</p>
  
  <pre><code>UserEntity userEntity = userEntityService.findById(UserEntity.class, new Long(id));
  model.addAttribute("userAttribute", userEntity);
  
  List&lt;RoleEntity&gt; roleList = roleEntityService.findAll();
  model.addAttribute("roleList", roleList);
  </code></pre>
  
  <p>Extract from the JSP:</p>
  
  <pre><code>&lt;form:form modelAttribute="userAttribute" method="POST" action="${saveUrl}"&gt;
  ...
  
      &lt;table align="center"&gt;
          &lt;tr&gt;
              &lt;td&gt;ID&lt;/td&gt;
              &lt;td&gt;Role Name&lt;/td&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${roleList}" var="role" varStatus="status"&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;form:checkbox path="roles" value="${role}" label="${role.id}" /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;c:out value="${role.name}" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  
  ...
  &lt;/form:form&gt;
  </code></pre>
  
  <p>The Spring MVC documentation says this:
  When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as 'checked' if the configured setValue(Object) value is present in the bound Collection.</p>
  
  <p>Isn't that the case here? What am I missing here?</p>
  
  <p>Thanks a lot.</p>
  
  <p>Paul</p>