如何使用自定义验证器在Spring MVC中一次验证bean的单个属性?

时间:2014-08-01 08:24:57

标签: java spring validation spring-mvc

我需要通过电子邮件发送忘记密码链接,一旦用户输入了应该只进行验证的电子邮件。我只有一个POJO具有属性电子邮件,密码,确认密码。

同样,当用户点击发送的链接时,显示输入密码并确认密码的表单现在只需要验证这两个属性。我只有一个自定义验证器来执行验证。

有没有办法做到这一点?以下是我的代码

User.java

public class User {

private String name;
private String password;
private String confirmPassword;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getConfirmPassword() {
    return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;
}

MyController.java摘录

@RequestMapping(value="/forgot", method=RequestMethod.POST)
public ModelAndView forgotPasswordSubmit(@Valid @ModelAttribute("User")  User,BindingResults result, user,HttpServletRequest request) {

    ModelAndView model = new ModelAndView("pwd/forgot/index");
    // to check user information has any error
 if(results.hasErrors())
        return model;
    model.addObject(user);
    boolean check = service.validate(user);
    if (check == true)
        return model;
    String status = service.sendPasswordResetLink(accountId, user, request);
    if (status.equals("success")) {
        return new ModelAndView("pwd/forgot-res/index");

    }

    return model;
}

当用户点击请求发送

@RequestMapping(value="/reset", method=RequestMethod.POST)
public ModelAndView resetPasswordSubmit(@ModelAttribute("linkId") String linkId,
    @Valid @ModelAttribute("user")User user,BindingResult results,HttpServletRequest request) {

    String view = "pwd/reset-success/index";
    if(results.hasErrors())
        return model;
    String status=  this.service.updateUserPassword(linkId,user.getPassword(),user.getConfirmPassword(),request);
    if(!status.equals("success"))
        return new ModelAndView("pwd/reset-failed/index");
    //if Reset Failed
      // view = "pwd/reset-failed/index";

    ModelAndView model = new ModelAndView(view);


    return model;
}

1 个答案:

答案 0 :(得分:1)

您的密码表单和链接后的确认密码应如下所示。

<form>
    Password : <input type="password" name="password"/>
    Confirm Password : <input type="password" name="confirmPassword"/>
    <input type="hidden" name="email" value="N/A"/>
    <input type="submit" value="Submit"/>
</form>

您的验证器应如下所示,您在email属性中检查您发送的隐藏值。如果匹配,那么您知道在链接点击电子邮件后您在表单中。如果不是以前的形式。基于此,您可以进行验证。

public class UserValidator implements Validator
{

    @Override
    public boolean supports(Class<?> clazz)
    {
        return User.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors)
    {
        User user = (User) target;
        if (user.getEmail().equals("N/A")) {
            // Password Reset Form Validation (Password, Confirm Password)
        } else {
            // Password Reset Request Form Validation (Email, Password, Confirm Password)
        }
    }
}