1

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

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.