通过 @ModelAttribute Spring mvc 使用 Set<Object> 映射下拉字段

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

Follwoing 是我的 Pojo 类,表单字段通过 @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;
    }   

}

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

表格如下:-

<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>

下面是我的控制器:-

@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);
    }

问题是每个表单字段都映射到 Pojo class 的变量,但 Set 没有被映射。它正在制作 Collection Set 的空对象。我想用角色 ID 映射下拉列表的值。请帮忙

我做到了。 实际上 @ModelArttribute 不会将嵌套对象映射到 Set 集合。我将 Set 替换为 List 并将表单字段重命名为 roles[0].id,其中 id 是 Roles pojo class.

的变量