使用 initBinder 添加多个验证器 [英] Adding multiple validators using initBinder
问题描述
我正在使用 initBinder
方法添加用户验证器:
I'm adding a user validator using the initBinder
method:
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserValidator());
}
这是UserValidator
public class UserValidator implements Validator {
public boolean supports(Class clazz) {
return User.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
User u = (User) target;
// more code here
}
}
validate
方法在控制器方法调用期间被正确调用.
The validate
method is getting properly called during the controller method call.
@RequestMapping(value = "/makePayment", method = RequestMethod.POST)
public String saveUserInformation(@Valid User user, BindingResult result, Model model){
// saving User here
// Preparing CustomerPayment object for the payment page.
CustomerPayment customerPayment = new CustomerPayment();
customerPayment.setPackageTb(packageTb);
model.addAttribute(customerPayment);
logger.debug("Redirecting to Payment page.");
return "registration/payment";
}
但在返回付款屏幕时出现此错误:
But while returning to the payment screen I'm getting this error:
java.lang.IllegalStateException:验证器 [com.validator.UserValidator@710db357] 的目标无效:com.domain.CustomerPayment[customerPaymentId=null]org.springframework.validation.DataBinder.setValidator(DataBinder.java:476)com.web.UserRegistrationController.initBinder(UserRegistrationController.java:43)sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)org.springframework.web.bind.annotation.support.HandlerMethodInvoker.initBinder(HandlerMethodInvoker.java:393)org.springframework.web.bind.annotation.support.HandlerMethodInvoker.updateModelAttributes(HandlerMethodInvoker.java:222)org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:429)org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
java.lang.IllegalStateException: Invalid target for Validator [com.validator.UserValidator@710db357]: com.domain.CustomerPayment[ customerPaymentId=null ] org.springframework.validation.DataBinder.setValidator(DataBinder.java:476) com.web.UserRegistrationController.initBinder(UserRegistrationController.java:43) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.initBinder(HandlerMethodInvoker.java:393) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.updateModelAttributes(HandlerMethodInvoker.java:222) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:429) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
这可能是因为我要返回一个 CustomerPayment
而没有为此定义验证器.
This might be because I'm returning a CustomerPayment
and there is not validator defined for that.
我也无法在 initBinder
方法中添加多个验证器.
I'm also not able to add multiple validators in initBinder
method.
我该如何解决这个问题?
How can I fix this?
推荐答案
您需要将 @InitBinder
注释的值设置为您希望它验证的命令的名称.这告诉 Spring 将绑定器应用于什么;没有它,Spring 会尝试将它应用到所有事物中.这就是您看到该异常的原因:Spring 正在尝试将绑定器(使用您的 UserValidator
)应用于 CustomerPayment
类型的参数.
You need to set the value of the @InitBinder
annotation to the name of the command you want it to validate. This tells Spring what to apply the binder to; without it, Spring will try to apply it to everything. This is why you're seeing that exception: Spring is trying to apply the binder - with your UserValidator
- to a parameter of type CustomerPayment
.
在您的特定情况下,您似乎需要以下内容:
In your specific case, it looks like you need something like:
@InitBinder("user")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserValidator());
}
对于您的第二个问题,正如 Rigg802 所解释的,Spring 不支持将多个验证器附加到单个命令.但是,您可以为不同的命令定义多个 @InitBinder
方法.因此,例如,您可以将以下内容放入单个控制器并验证您的用户和付款参数:
To your second question, as Rigg802 explained, Spring does not support attaching multiple validators to a single command. You can, however, define multiple @InitBinder
methods for different commands. So, for example, you could put the following in a single controller and validate your user and payment parameters:
@InitBinder("user")
protected void initUserBinder(WebDataBinder binder) {
binder.setValidator(new UserValidator());
}
@InitBinder("payment")
protected void initPaymentBinder(WebDataBinder binder) {
binder.setValidator(new CustomerPaymentValidator());
}
这篇关于使用 initBinder 添加多个验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!