Search code examples
jspspring-mvcspring-form

mapping drop down field with Set<Object> via @ModelAttribute Spring mvc


Follwoing is my Pojo Classes to which form field is mapped via @ModelAttribute:-

 public class WebUsers extends BaseDomain {

    private int webUserId;
    private String firstName;
    private String lastName;
    private String userLogin;
    private String password;
    private UserDetail userDetail;
    private Set<Roles> roles;
    private boolean isTestUser;
    private boolean isActive;

    public int getWebUserId() {
        return webUserId;
    }
    public void setWebUserId(int webUserId) {
        this.webUserId = webUserId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getUserLogin() {
        return userLogin;
    }
    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public UserDetail getUserDetail() {
        return userDetail;
    }
    public void setUserDetail(UserDetail userDetail) {
        this.userDetail = userDetail;
    }

    public Set<Roles> getRoles() {
        return roles;
    }
    public void setRoles(Set<Roles> roles) {
        this.roles = roles;
    }
    public boolean isTestUser() {
        return isTestUser;
    }
    public void setTestUser(boolean isTestUser) {
        this.isTestUser = isTestUser;
    }
    public boolean isActive() {
        return isActive;
    }
    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }   

}

Roles Pojo:-

public class Roles extends BaseDomain{

    @NotBlank
    private int id;
    private String role;
    private String roleDescription;
    private boolean isActive;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRoleDescription() {
        return roleDescription;
    }

    public void setRoleDescription(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

Following is the form :-

<form role="form" method="post" action="registerWebUser">
    <div class="form-group">
    <label>First Name</label>
    <input class="form-control" name="firstName">                                        
     </div>
     <div class="form-group">
     <label>Last Name</label>
     <input class="form-control" name="lastName">                                        
     </div>
     <div class="form-group">
     <label>Email Id</label>
     <input class="form-control" type="email" name="userDetail.emailId">
     <p class="help-block">use it as user login</p>
     </div>                                        
     <div class="form-group">
     <label>Password</label>
     <input class="form-control" name="password">
     </div>                                        
     <div class="form-group">
     <label>Select Role</label>
     <select class="form-control" name="roles.id">
     <option value="1">Super Admin</option>
     <option value="3">Admin</option>
     <option value="4">Manager</option>
     <option value="5">Android Developer</option>                                                

      </select>
      </div>

     <button type="submit" class="btn btn-default">Register</button>
     <button type="reset" class="btn btn-default">Reset Form</button>
     </form>

And Below is my Controller:-

@RequestMapping(value = "/registerWebUser", method = RequestMethod.POST)
public ModelAndView registerWebUserHandler(@ModelAttribute WebUsers webUsers) {
        getLogger().info("roles object ==  " + webUsers.getRoles());
        WebUsersDTO dto = copyDtoInDomain(webUsers);
        int id = adminService.createAdminService(dto);

        Map<String, String> responseMap = new HashMap<String, String>();
        if (id != 0) {
            responseMap.put("response", "Successfully Registered");
        } else {
            responseMap.put("response", "failed");
        }

        return new ModelAndView("registration-success", responseMap);
    }

Problem is every form field is mapped with variable of Pojo class but Set is not getting mapped. it is making an empty object of Collection Set. I want to map the value of drop down with id of Roles. Please help


Solution

  • I did it. Actually @ModelArttribute do not map nested objects with Set collection. I replace the Set with List and rename the form field like roles[0].id where id is variable of Roles pojo class.