java自定义表单选择中的项目
在我的注册表中,我有一个字段,允许用户选择角色。但是,我只想展示两个角色:用户和主持人。 但是,我不知道该怎么做。 此注册表格:
<div class="row">
<div class="form-group col-md-12">
<label style="color:#ffffff;" class="col-md-3 control-lable" for="userProfiles">Roles</label>
<div class="col-md-7">
<form:select path="userProfiles" items="${roles}" multiple="true" itemValue="id" itemLabel="type" class="form-control input-sm" />
<div class="has-error">
<form:errors path="userProfiles" class="help-inline"/>
</div>
</div>
</div>
</div>
我有一个enum:UserProfileType
public enum UserProfileType implements Serializable{
USER("USER"),
DBA("DBA"),
ADMIN("ADMIN"),
MODERATOR("MODERATOR");
String userProfileType;
private UserProfileType(String userProfileType){
this.userProfileType = userProfileType;
}
public String getUserProfileType(){
return userProfileType;
}
}
这就是它的样子:
这是我的管理员,负责注册的那部分
//new user
@RequestMapping(value = "/registrationForm", method = RequestMethod.GET)
public String newUser(User user, ModelMap model) {
//User user = new User();
model.addAttribute("user", user);
model.addAttribute("loggedinuser", getPrincipal());
return "registrationForm";
}
//save user
@RequestMapping(value = { "/registrationForm" }, method = RequestMethod.POST)
public String saveUser1(@Valid User user, BindingResult result,
ModelMap model) {
if (result.hasErrors()) {
return "registrationForm";
}
if(!userService.isUserSSOUnique(user.getId(), user.getSsoId())){
FieldError ssoError =new FieldError("user","ssoId",messageSource.getMessage("non.unique.ssoId", new String[]{user.getSsoId()}, Locale.getDefault()));
result.addError(ssoError);
return "registrationForm";
}
userService.saveUser(user);
model.addAttribute("success", "User " + user.getFirstName() + " "+ user.getLastName() + " registered successfully");
model.addAttribute("loggedinuser", getPrincipal());
//return "success";
return "registrationsuccess";
}
我正在使用spring mvc/security和hibernate。 提前谢谢
# 1 楼答案
页面显示的数据应由控制器提供和操作。在移交给视图之前,过滤控制器中的角色
根据向模型中添加属性的方式,可以按如下方式进行(或者按Spring MVC灵活的任何等效方式)
您可以在Enum类中创建方法,根据需要返回过滤后的值:
您还可以在视图中筛选角色,但不建议在视图中使用控制逻辑