在 Spring MVC 控制器中的多个类中应用自定义验证器
我有一个使用自定义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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做
然后你的验证应该做这样的事情
我认为你不应该使用这个可能更好地用于分离类,这样可以更好地提高可读性,并降低复杂性。在验证器或模型对象(访问者模式)中引入层次结构并不是最好的解决方案。
You could do this
Then your validate should do something like this
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.
看来您只想验证 2 个类,因为您想重复“确认密码”验证,其中您有 2 个应该匹配的密码字段。相反,为什么不简单地将接口传递给验证器呢?
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?