Search code examples
springcheckboxdata-bindingspring-form

How to bind Spring form:checkbox instead of form:checkboxes?


I am having problems with form:checkbox. I cannot make it display selected values. When I selected values and submit, correct values are display in database. When I load page all values (checkboxes) are not selected.

Elements below are located inside this:

<form:form role="form" commandName="user" class="form-horizontal" action="${form_url}">
</form:form>

This works just fine:

<form:checkboxes items="${availableRoles}" path="roles" itemLabel="role" itemValue="id" element="div class='checkbox'"/>                    

This doesn't work:

<c:forEach items="${availableRoles}" var="r" varStatus="status">
    <div class="checkbox">
        <form:checkbox path="roles" label="${r.description}" value="${r.id}"/>
    </div>
</c:forEach>

This is my domain class:

public class User {
    private List<Role> roles;

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

This is my custom property editor:

public class RolePropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) {
        Role role = new Role();
        role.setId(Integer.valueOf(text));
        setValue(role);
    }

}

Controller has this method:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Role.class, new RolePropertyEditor());
}

Controller method:

@RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
public String updateUser(@PathVariable Integer userId, Model model) {
    User user = userService.getByUserId(userId);
    List<Role> availableRoles = roleService.getAllRoles();

    model.addAttribute("availableRoles", availableRoles);
    model.addAttribute("user", user);

    return "user/update";
}

Solution

  • After debugging session I found the solution.

    Because of Spring internals JSP should look like this:

    <c:forEach items="${availableRoles}" var="r">
        <div class="checkbox">                          
            <form:checkbox path="roles" label="${r.description}" value="${r}"  />
        </div>
    </c:forEach>
    

    Notice that value is item (r), not item's member like r.id.

    Also you need getAsText implementation in your custom PropertyEditor.

    @Override
    public String getAsText() {
        Role role = (Role) this.getValue();
        return role.getId().toString();
    }