I have custom validator and I register it in my controller
@Controller
public class MyController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo) { ... }
}
but I want to register in other controllers also,so to be able just to write @Valid and the Foo object to be validated. From what I see I understand that I can use @ControllerAdviced class which to register the validator on every controller, or to use
<mvc:annotation-driven validator="globalValidator"/>
But how to register my validator, how Spring understand which Validator I want to make global one? Scans for every implementing Validator class? Can I do it with xml configuration? How to use this approach?
I do not understand the Spring's description:
The alternative is to call setValidator(Validator) on the global WebBindingInitializer. This approach allows you to configure a Validator instance across all annotated controllers. This can be achieved by using the SpringMVC namespace:
xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xss http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven validator="globalValidator"/>
globalValidator
... Or the one namedfooBar
if you write<mvc:annotation-driven validator="fooBar"/>
– M. Deinum