编辑

时间:2015-08-12 05:58:23

标签: java spring validation spring-mvc

我在Spring MVC中使用简单的CRUD应用程序时出现问题。我的域用户类有一个名为login的属性。

@Size(min = 6, max = 30, message = "{addUser.validation.login}")
@UserLogin
private String login;

我还有验证器,检查数据库中是否存在注册登录:

@Component
public class UserLoginValidator implements ConstraintValidator<UserLogin, String> {

    @Autowired
    private UserService userService;

    @Override
    public void initialize(UserLogin a) {

    }

    @Override
    public boolean isValid(String t, ConstraintValidatorContext cvc) {

        User user;
        try {
            user = (User) userService.getUserByLogin(t);

        } catch (Exception e) {
            return true;
        }

        if (user != null) {
            return false;
        }
        return false;
    } 
}

在我的控制器课程中,我有方法:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String getAddNewCustomerForm(@ModelAttribute("addUser") User addUser) {
       return "addUser";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String processAddNewCustomerForm(Model model, @ModelAttribute("addUser") @Valid User user, BindingResult result) {

       if (result.hasErrors()) {
          return "addUser";
       }

        if (user.getUserID() == 0) {
           user.setCreateDate(new Date());
           userService.addUser(user);
        } else {
           user.setCreateDate(new Date());
           userService.editUser(user);
        }

           return "redirect:/users";
}

@RequestMapping(value = "{id}/edit")
public String editUser(Model model, @PathVariable int id) {

       User user = new User();

       user = this.userService.getUserById(id);
       user.setPassword2(user.getPassword());
       model.addAttribute("addUser", user);
       model.addAttribute("users", userService.getAllUsers());
       return "addUser";
}

@InitBinder
public void initialiseBinder(WebDataBinder binder) {
       binder.setValidator(userValidator);
}

现在我的问题是如何禁用登录属性验证 - 仅在编辑时? 这是否是一种简单的方法,除了单独的方法和视图以及相应的编辑和添加方法?

1 个答案:

答案 0 :(得分:0)

您可以创建2个不同的Controller类并添加

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
    binder.setAllowedFields("login", "someAnotherField"...);
}

包括或排除验证登录。

如果您只需要一个课程,可以尝试使用POST / PUT进行创建和编辑

@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
    String httpMethod = request.getMethod();
    if ("POST".equals(httpMethod)) {
        // registration
        binder.setAllowedFields("name", "email", "password");
    } else if ("PUT".equals(httpMethod)) {
        // update
        binder.setAllowedFields("email", "password");
    }

}

@RequestMapping(value = "/user", method = RequestMethod.POST)
public String create(@Valid User user, BindingResult result,
        ModelMap modelMap) {
    //registration...
}

@RequestMapping(value = "/user", method = RequestMethod.PUT)
public String create(@Valid User user, BindingResult result,
        ModelMap modelMap) {
    //update...
}

如上所述here