ok, we are trying out spring 3 rc1 and have run into some interesting behavior.
The first is a bit of a warning to people using binder.setValidator, if you are returning some object of different types into the page make sure you use
@InitBinder("ourBackingObject") rather then
@InitBinder for setting your Validator...
The reason is that InitBinder is called for every object being placed into a page.
While that isn't a bad thing, it checks the validators supports method when the validator is set... throwing an exception if it is of the wrong type.
Which can be bad, because if you have 2 objects being placed on the page, one being your backing object, and one being something else, it will fail when it calls setValidator... since the InitBinders target is not what the validator expects.
an example.
if you had a backing Object of type Foo you would expect to be able to..
and you can... but if you put a Bar object into your ModelAndView then InitBinder will be called with a target of your Bar object.Code:@InitBinder void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); }
and setValidator will fail.
however if you have
then all is good with the world, since it will only be called for the "foo" object.Code:@InitBinder("foo") void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); }
ok, the part I can't get working with the validator...
when FooValidator fails, (because one of foo's field is invalid) it throws its toys out of the cot...Code:@InitBinder("foo") void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); } @RequestMapping(method = POST) ModelAndView create(@Valid @ModelAttribute Foo foo, BindingResult binder) { // whatever it won't get then far.... }
so the @Valid annotation is working, Foo is being Validated, but...Code:org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'foo' on field 'code': rejected value [a]; codes [code.isTooShort.foo.code,code.isTooShort.code,code.isTooShort.java.lang.String,code.isTooShort]; arguments []; default message [null] at org.springframework.web.bind.support.WebRequestDataBinder.closeNoCatch(WebRequestDataBinder.java:116) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:686) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:288) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:156) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:378) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:366) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:781) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:556) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
The validation is failing, but... it crashes out rather then the method being called....
hell even having
won't save it.Code:@ExceptionHandler(BindException.class) String failedValidate() { return "view"; }
so...
Does anyone have a demo with @Valid and setValidator working so we can see what we are doing wrong? Oh as a note, we can not annotate the Entity, so....
--- Blair


Reply With Quote
