我正在尝试为我的应用程序实现创建/编辑用户。
在我的控制器上,我有以下方法来创建新用户或编辑现有用户。
@RequestMapping(value = "/admin/createUser", method = RequestMethod.GET)
public void createtUser(ModelMap model) throws IOException, Exception {
model.addAttribute("pojo", new UserPojo());
model.addAttribute("roles", initializeProfiles());
}
@ModelAttribute("roles")
public List<UserProfile> initializeProfiles() {
return userProfileService.findAll();
}
@RequestMapping(value = "/admin/createUser", method = RequestMethod.POST)
public String createUserPost(ModelMap model, @Valid @ModelAttribute("pojo") UserPojo pojo, BindingResult result, Errors errors) {
if (errors.hasErrors()) {
System.out.println("There are errors");
return "/admin/createUser";
}
System.out.println(pojo.getProfiles());
try {
userService.save(pojo.getSsoId(),
pojo.getFirstName(),
pojo.getLastName(),
pojo.getEmail(),
pojo.getProfiles(),
pojo.getPassword());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("success", "User " + pojo.getSsoId() + " has been created successfully");
return "redirect:/admin/homeAdmin";
}
UserPojo
模型是:
public class UserPojo {
private String id;
private String ssoId;
private String firstName;
private String lastName;
private String email;
private String password;
private String state;
private Set<UserProfile> profiles;
// getters and setters
UserProfile
pojo是:
@Entity
@Table(name="userProfile")
public class UserProfile extends BasePojo {
@Column(name="type", length=15, unique=true, nullable=false)
private String type = UserProfileType.USER.getUserProfileType();
// getter and setter
UserProfileType
枚举是:
public enum UserProfileType {
USER("USER"),
DBA("DBA"),
ADMIN("ADMIN");
String type;
private UserProfileType(String userProfileType) {
this.type = userProfileType;
}
public String getUserProfileType() {
return type;
}
在我的 createUser.html
页面上,我选择了以下用户:
<select class="form-control" th:field="*{profiles}" name="profiles" multiple="multiple">
<option th:each="role : ${roles}"
th:value="${{profiles}}"
th:text="${role.type}">
</option>
</select>
问题是,当我提交数据时,控制器在UserPojo
个配置文件字段中看到空,并且不会保留指定的用户配置文件。
如何解决这个问题?
答案 0 :(得分:1)
首先,我认为你需要在这里更改映射:
@Entity
@Table(name="userProfile")
public class UserProfile extends BasePojo {
@Enumerated(EnumType.STRING)
@Column(name="type", length=15, unique=true, nullable=false)
private UserProfileType type = UserProfileType.USER;
// getter and setter
何时,我猜<options>
上有错误:
th:value="${{profiles}}"
如果roles
是来自枚举的值,您可以尝试:
<option th:each="role : ${roles}"
th:value="${role.type.name()}" // or role.type (If you don't want to change mapping)
th:text="${role.type}">
</option>
按名称获取枚举值:
public enum UserProfileType {
USER("USER"),
DBA("DBA"),
ADMIN("ADMIN");
String type;
private UserProfileType(String userProfileType) {
this.type = userProfileType;
}
public String getUserProfileType() {
return type;
}
public UserProfileType getByName(String name) {
return UserProfileType.valueOf(name);
}
您还需要UserProfileType
类型的格式化程序:
public class UserProfileTypeFormatter implements Formatter<UserProfileType> {
public UserProfileTypeFormatter() {
super();
}
@Override
public String print(UserProfileType object, Locale locale) {
return object.getType();
}
@Override
public UserProfileType parse(String text, Locale locale) throws ParseException {
return UserProfileType.getByName(text);
}
}