【问题标题】:apply custom validator in spring MVC controller in more than one class在多个类中的 Spring MVC 控制器中应用自定义验证器
【发布时间】:2011-07-13 11:59:46
【问题描述】:

我有一个使用自定义 valiadtor 的注册页面

public class CustomValidator implements Validator {

    private Validator validator;

    public CustomValidator(Validator validator) {
        this.validator = validator;
    }

    @SuppressWarnings("rawtypes")
    public boolean supports(Class clazz) {
        return Registration.class.equals(clazz);
    }

    public void validate(Object target, Errors errors) {

        validator.validate(target, errors);



        Registration myModel1 = (Registration) target;
        if (! myModel1.getConfirm_password().equals(myModel1.getPassword())) {
            errors.rejectValue("confirm_password", "confirm_password.confirm");
        }

    }
}

问题是我想在两种形式上应用它,所以我很困惑如何用两个类编写这个函数。这个函数现在只有 Registration 类。如果我也想要 Person 类呢?

public boolean supports(Class clazz) {
        return Registration.class.equals(clazz);
    }

我可以在该函数中编写多个类

这是我的控制器

@InitBinder
    public void initBinder(final WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyy"), true));
        Validator validator = (Validator) binder.getValidator();
        binder.setValidator(new CustomValidator((org.springframework.validation.Validator) validator));
    }

【问题讨论】:

    标签: java validation spring-mvc


    【解决方案1】:

    你可以这样做

    public boolean supports(Class clazz) {
        return Registration.class.equals(clazz) || Another.class.equals(clazz);
    }
    

    那么你的验证应该做这样的事情

        public void validate(Object target, Errors errors) {
        validator.validate(target, errors);
    
        String password = null;
        String confirm = null;
        if (target instanceof Registration) {
            Registration registration = (Registration) target;
            password = registration.getPassword();
            confirm = registration.getConfirm_password();
        } else if (target instanceof Another) {
            Another another = (Another) target;
            password = another.getPassword();
            confirm = another.getConfirm_password();
        }
        if (! confirm.equals(password())) {
            errors.rejectValue("confirm_password", "confirm_password.confirm");
        }
    
    }
    

    我认为您不应该使用它,可能最好使用分隔类更好地提高可读性并降低复杂性。在您的验证器或模型对象(Vistor 模式)中引入层次结构并不是最好的解决方案。

    【讨论】:

    • 我正在考虑为同一个对象实现一组不同的验证。选择的验证基于用户事件,例如单击按钮。你能告诉我在这个例子中这是否可能,因为我看到一个对象的一个​​实例只能有一组为其定义的验证规则
    【解决方案2】:

    您似乎只想验证 2 个类,因为您想在有 2 个应该匹配的密码字段的地方复制“确认密码”验证。相反,为什么不简单地将接口传递给验证器?

    public void validate(IPasswordContainer target, Errors errors) {
        if(!target.getConfirm_password().equals(target.getPassword())) {
            errors.rejectValue("confirm_password", "confirm_password.confirm");
        }
    }
    
    
    public boolean supports(Class clazz) {
        return IPasswordContainer.class.equals(clazz);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2018-10-04
      • 2019-04-21
      • 1970-01-01
      • 2012-11-20
      • 2014-01-02
      • 2014-09-24
      相关资源
      最近更新 更多