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;
}
}
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;
}
以下是表格: -
<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类的变量进行映射,但Set未映射。它正在制作Collection Set的空对象。我想将下拉值与Roles的id映射。请帮忙
答案 0 :(得分:0)
我做到了。 实际上@ModelArttribute不会使用Set集合映射嵌套对象。我将Set替换为List并重命名表单字段,如roles [0] .id,其中id是Roles pojo类的变量。