Announcement Announcement Module
Collapse
No announcement yet.
List<Object> in checkboxes Spring 3 MVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Code:
    @Controller
    @RequestMapping("/users")
    public class UserController{
        ...
         @RequestMapping("/add", method=RequestMethod.GET)
         public String showRegistrationForm(ModelMap model){		
    		User user = new User();		
    		model.addAttribute("roleList", RoleUtil.getRoles());		
    		model.addAttribute(user);		
    		return "addUser";
    	}
    
          @RequestMapping("/save", method=RequestMethod.POST)
          public ModelAndView saveUser(@ModelAttribute("user")User user, BindingResult result){
    		if(user.getRoles()!=null){
    			for(Roles r : user.getRoles()){
    				System.out.println(r.getName());
    			}
    		}					
    		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)
    Code:
    <form:form method="POST" action="${saveUserUrl}" modelAttribute="user">
        <form:label path="uName">Username:</form:label>
    		<form:input path="uName"/>
    		<br/>
    		<form:label path="uPwd">Password:</form:label>
    		<form:input path="uPwd"/>
    		<br/>
    		Roles:
     		<form:checkboxes items="${roleList}" path="roles" itemLabel="name"/>		
    		<br/>
    		<input type="submit" value="Save"/>
    </form:form>
    and the model (User.java and Role.java)
    Code:
    @Entity
    public class User implements Serializable{
            private Long userId;
            private String uName;
            private String uPwd
            private List<Role> roles;
    
           ... getters and setters
    }
    
    @Entity
    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
Working...
X