我正在尝试为用户管理构建一个简单的CRUD。所以我有两个基本的豆子:
public class User {
private static final long serialVersionUID = 1L;
private Long userId;
private String login;
private String password;
private Set<Profile> profiles;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId= userId;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String pass) {
this.password = pass;
}
public Set<Profile> getProfiles() {
return profiles;
}
public void setPerfiles(Set<Profile> perfiles) {
this.profiles= profiles;
}
}
并且
public class Profile{
private static final long serialVersionUID = 1L;
private Long profileId;
private String name;
public Profile() {
}
public Profile(String name) {
this.name= name;
}
public Long getProfileId() {
return this.profileId;
}
public void setProfileId(Long profileId) {
this.profileId= profileId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name= name;
}
}
要创建新用户,我创建了一个表单,其中User作为modelAttribute:
<f:form id="formCreateUser" action="${urlBase}users/saveUser"
method="post" modelAttribute="user">
<fieldset>
<div class="edit-form">
<f:label path="login">Login</f:label>
<f:input path="login" id="login" class="text ui-widget-content ui-corner-all" />
</div>
<div class="edit-form-multiple-select">
<label>Available profiles</label>
<select id="availableProfiles" multiple="multiple" class="ui-widget-content ui-corner-all" >
<c:forEach var="profile" items="${availableProfiles}" >
<option value="${profile.profileId}" ><s:message code="profile.${profile.name}"/></option>
</c:forEach>
</select>
</div>
<div class="edit-form-multiple-select-arrows" >
<a id="moveRight" href="#" class="form-button" style="display: inline-block; margin: 5px 0px; ">>></a>
<br>
<a id="moveLeft" href="#" class="form-button"><<</a>
</div>
<div class="edit-form-multiple-select">
<label>Actual profiles</label>
<f:select id="profiles" path="profiles" multiple="true" cssClass="ui-widget-content ui-corner-all">
<c:forEach var="profile" items="${user.profiles}" >
<f:option value="${profile.profileId}" ><s:message code="profile.${profile.name}"/></f:option>
</c:forEach>
</f:select>
</div>
</fieldset>
此表单有两个选择:
当我想向一个用户添加一些配置文件时,我将选项从availableProfiles选择移动到配置文件选择。
问题是,如果我将一个或多个配置文件从一个选项移动到另一个选项后提交表单,我将无法在Controller上映射这些配置文件。 这是Controller方法:
@RequestMapping("/saveUser")
public String saveUser(@ModelAttribute User user, BindingResult bindingResult, Model model, HttpServletRequest request){
try{
System.out.println(user.getProfiles()); // Here profiles are null
return "editUser";
}
catch(Exception e){
return "listUser";
}
}
PD:我尝试使用Formatter,CustomCollectionEditor和PropertyEditor将profileId绑定到Set而没有成功。