[英]Understanding “globalValidator” in Spring MVC
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. 但我也希望在其他控制器中注册,以便能够编写@Valid和Foo对象进行验证。 From what I see I understand that I can use @ControllerAdviced class which to register the validator on every controller, or to use
从我看到的我明白我可以使用@ControllerAdviced类来在每个控制器上注册验证器,或者使用
<mvc:annotation-driven validator="globalValidator"/>
But how to register my validator, how Spring understand which Validator I want to make global one? 但是如何注册我的验证器,Spring如何理解我想制作全局验证器? Scans for every implementing Validator class?
扫描每个实现Validator类? Can I do it with xml configuration?
我可以用xml配置吗? How to use this approach?
如何使用这种方法?
I do not understand the Spring's description: 我不明白Spring的描述:
The alternative is to call setValidator(Validator) on the global WebBindingInitializer.
另一种方法是在全局WebBindingInitializer上调用setValidator(Validator)。 This approach allows you to configure a Validator instance across all annotated controllers.
此方法允许您跨所有带注释的控制器配置Validator实例。 This can be achieved by using the SpringMVC namespace:
这可以通过使用SpringMVC命名空间来实现:
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 ">
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"/>
The documentation is quite clear on the Validation section : 验证部分的文档非常清楚:
In Spring MVC you can configure it for use as a global Validator instance, to be used whenever an @Valid or @Validated controller method argument is encountered , and/or as a local Validator within a controller through an @InitBinder method .
在Spring MVC中,您可以将其配置为用作全局Validator实例,在遇到 @Valid或@Validated控制器方法参数时使用 , 和/或通过@InitBinder方法在控制器中作为本地Validator使用。 Global and local validator instances can be combined to provide composite validation
可以组合全局和本地验证器实例以提供复合验证
If I understand correctly in your example the FooValidator you want to use it upon every validation as global Validator so define it as a bean and inject it as you show directly in the mvc:annotation-driven
XML entry as you are showing already. 如果我在您的示例中正确理解了FooValidator,您希望在每次验证时将其用作全局Validator,因此将其定义为bean并将其注入直接显示在
mvc:annotation-driven
XML条目中,如您所示。
On top of that per-Controller you can have custom (applied on top only on that Controller-responsible forms) via the @InitBinder
annotation. 在每个控制器之上,您可以通过
@InitBinder
注释进行自定义(仅在控制器负责的表单上应用)。
As a side note, in your @RequestMapping
method receiving the POST request where your @Valid
parameter is: You can have a BindingResult
entry right after that to take decisions on routes etc. In your example: 作为旁注,在
@RequestMapping
方法中接收POST请求,其中@Valid
参数为:您可以在此之后拥有BindingResult
条目以对路由等做出决策。在您的示例中:
@RequestMapping("/foo", method=RequestMethod.POST)
public String processFoo(@Valid Foo foo, BindingResult result) {
if(result.hasErrors()) {
return "go/that/way";
}
//..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.