Announcement Announcement Module
No announcement yet.
List<Object> in checkboxes Spring 3 MVC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • List<Object> in checkboxes Spring 3 MVC

    Hi i recently started to learn Spring 3, and now I'm developing a small project as exercise but i have a problem/doubt in my small project:

    The model is based in JPA Entities (User and Roles), to create a user I need to assign user name, password and roles (through checkboxes). But I'm very confused about getting the results from the marked checkboxes, here's my code so far:

    The Controller
    public class UserController{
         @RequestMapping("/add", method=RequestMethod.GET)
         public String showRegistrationForm(ModelMap model){		
    		User user = new User();		
    		model.addAttribute("roleList", RoleUtil.getRoles());		
    		return "addUser";
          @RequestMapping("/save", method=RequestMethod.POST)
          public ModelAndView saveUser(@ModelAttribute("user")User user, BindingResult result){
    			for(Roles r : user.getRoles()){
    		return "home";
    The RoleUtil.getRoles() returns a Map<String,Role> with the three possible roles: {"Admin":ROLE_ADMIN, "User":ROLE_USER, "Employee":ROLE_EMPLOYEE}

    The View(addUser.jsp)
    <form:form method="POST" action="${saveUserUrl}" modelAttribute="user">
        <form:label path="uName">Username:</form:label>
    		<form:input path="uName"/>
    		<form:label path="uPwd">Password:</form:label>
    		<form:input path="uPwd"/>
     		<form:checkboxes items="${roleList}" path="roles" itemLabel="name"/>		
    		<input type="submit" value="Save"/>
    and the model ( and
    public class User implements Serializable{
            private Long userId;
            private String uName;
            private String uPwd
            private List<Role> roles;
           ... getters and setters
    public class Role implements Serializable{
            private Long roleId;
            private String name;
            ...getters and setters
    When the server receives the submitted form, the user object loses the reference to previously inserted roles (the roles property is setted to null).

    1. In the examples that I've seen, the form backing object define an array (not a List nor a Map) to process the data submitted, It's this the only way to receive data from the checkboxes tag?

    2. In this other thread I note that they're submitting the form through PUT, so when I tried this this, Spring didn't find a mapping tough i changed the method attribute in the @RequestMapping