Mehrere Validatoren mit initBinder hinzufügen

Ich bin das hinzufügen einer Benutzer-validator mit der initBinder Methode:

@InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new UserValidator());
    }

Hier ist die 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
    }
}

Den validate Methode ist immer richtig aufgerufen, während der controller-Methode aufrufen.

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

Aber während der Rückkehr in den Zahlung-Bildschirm bin ich immer diese Fehlermeldung:

java.lang.IllegalStateException: Invalid target für Prüfer [com.validator.UserValidator@710db357]: com.domain.CustomerPayment[ customerPaymentId=null ]
org.springframework.die Validierung.DataBinder.setValidator(DataBinder.java:476)
com.web.UserRegistrationController.initBinder(UserRegistrationController.java:43)
Sonne.reflektieren.NativeMethodAccessorImpl.invoke0(Native-Methode)
Sonne.reflektieren.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
Sonne.reflektieren.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflektieren.Methode.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)

Diese könnte sein, weil ich fahre ein CustomerPayment und es ist nicht validator definiert.

Ich bin auch nicht in der Lage, mehrere Prüfungen hinzufügen in initBinder Methode.

Wie kann ich dieses Problem beheben?

InformationsquelleAutor der Frage Ravi | 2013-01-26

Schreibe einen Kommentar