Spring - 如何在创建和编辑用户时正确分配用户配置文件?
Spring - how to properly assign user profiles upon creating and editing users?
我正在尝试为我的应用程序实现一个 create/edit 用户。
在我的控制器上,我有以下方法来创建新用户或编辑现有用户。
@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 用户有:
<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
配置文件字段中看到 null,并且不会保留分配的用户配置文件。
如何解决这个问题?
首先,我认为您需要在此处更改映射:
@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);
}
}
我正在尝试为我的应用程序实现一个 create/edit 用户。
在我的控制器上,我有以下方法来创建新用户或编辑现有用户。
@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 用户有:
<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
配置文件字段中看到 null,并且不会保留分配的用户配置文件。
如何解决这个问题?
首先,我认为您需要在此处更改映射:
@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);
}
}