在 Spring MVC 控制器中的多个类中应用自定义验证器

发布于 2024-10-21 18:20:09 字数 1343 浏览 1 评论 0原文

我有一个使用自定义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));
    }

I have one registration page which uses custom 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");
        }

    }
}

The problem is that i want to apply it on two forms so i am confused how to write this function with two classes. This function now has only Registration class . what if i also want Person class in it as well

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

Can i write multiple classes in that function

here is my controller

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-10-28 18:20:10

你可以这样做

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

}

我认为你不应该使用这个可能更好地用于分离类,这样可以更好地提高可读性,并降低复杂性。在验证器或模型对象(访问者模式)中引入层次结构并不是最好的解决方案。

You could do this

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

Then your validate should do something like this

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

}

I don't think you should use this probably it is better to use to separate classes is better for readability, and reduces complexity. To introduce hierarchy in your validators or Model objects (Vistor pattern) is not the best solution.

甜妞爱困 2024-10-28 18:20:10

看来您只想验证 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);
}

It appears you only want to validate 2 classes because you want to duplicate the 'confirm password' validation where you have 2 password fields that should match. Instead, why not simply pass an interface to the validator?

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文