Hi,
In the following fragment of your code:
smallfish wrote
<ul>
<li th:each="role : ${resource.roles}"><input type="checkbox"
th:field="*{roles}" th:value="${role.id}" th:checked="${true}" /> <label
th:text="${role.name}">ROLE_USER</label></li>
</ul>
...you do not need that "th:checked" attribute at all. The th:field="*{roles}" attribute should take care of checking each of the values in the "roles" property of your form-backing bean so that, when one of the elements equals ${role.id} (the value set in the "th:value" attribute), thymeleaf will automatically write a checked="checked" attribute into your checkbox tag.
Trying to exemplify, let's say:
1. Your form-backing bean (th:object) is called "form"...
2. ...and form.getRoles() returns an array of Integers [23, 43, 76] (the roles currently selected)...
3. ...and role.getId() returns an Integer...
4. ...and resource.roles contains roles with ids [10, 20, 23, 43, 46, 76]
Thymeleaf will do the following:
1. Iterate for 10. 10 is not contained in form.getRoles() --> No checked attribute.
2. Iterate for 20. 20 is not contained in form.getRoles() --> No checked attribute.
3. Iterate for 23. 23 IS contained in form.getRoles() --> checked="checked" attribute.
4. Iterate for 45. 45 IS contained in form.getRoles() --> checked="checked" attribute.
5. Iterate for 46. 46 is not contained in form.getRoles() --> No checked attribute.
6. Iterate for 76. 76 IS contained in form.getRoles() --> checked="checked" attribute.
If this does not work for you, verify that:
1. The type of the array/list/set returned by form.getRoles() corresponds to the type returned by role.getId() (e.g. Integer, String, etc). This is, if form.getRoles() returns an array of Role objects (instead of Integers), then your th:value attribute should have "${role}" as value instead of "${role.id}".
2. The type returned by these methods correctly implements "equals()". Integer of course implements it, but if form.getRoles() returns Role[] and you rewrite th:value as "${role}", you should make sure the Role class implements "equals" so that Thymeleaf can know when a value is really contained in the roles array.
If after this explanation this still does not work for you, please give me more details about the type returned by form.getRoles() and role.getId() so that I can track any possible issues.
Regards,
Daniel.