在表单选择中自定义项目

时间:2016-05-18 11:31:19

标签: java spring forms spring-mvc

在我的注册表单中,我有一个字段,可以为用户提供选择角色的可能性。但是,我只想展示两个角色:USER和MODERATOR。 但是,我不知道该怎么办。 这个注册表格jsp:

<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>

我有一个枚举: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;
}

}

这就是它的样子:

registration form

这是我的控制员,负责注册的部分

//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 个答案:

答案 0 :(得分:1)

页面显示的数据应由控制器提供和操作。在移交给视图之前过滤控制器中的角色。

根据您向模型添加属性的方式,您可以像foLlows一样(或者与Spring MVC一样灵活的任何等效方式)

您可以在Enum类中创建方法,以根据您的需要返回过滤后的值:

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;
    }

    public static List<UserProfileType> getAdminRoles(){
        return Arrays.asList(UserProfileType.USER , UserProfileType.MODERATOR);
    }

}

@ModelAttribute("roles")
public List<UserProfileType> roles(){
   return UserProfileType.getAdminRoles();
}

您也可以在视图中过滤角色,但不建议您在视图中使用控制逻辑

<form:select  path="userProfiles"multiple="true"  class="form-control input-sm">
   <c:forEach items="${roles}" var = "role">
      <c:if test="${role.type eq 'USER' or role.type eq 'MODERATOR'}">
         <form:option value="${role.id}" label="${role.type}"/>
      </c:if>
   </c:forEach>
</form:select>