From ab10d37fae338d1709febb90654fed8bffbd33f6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 5 Oct 2009 22:24:42 +0000 Subject: [PATCH] Corrections regarding BindingResult(s); added a tip regarding programmatic use of LocalValidatorFactoryBean. --- spring-framework-reference/src/validation.xml | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 9e828563684..e394f842e0e 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -1270,7 +1270,7 @@ public class Person { This allows a javax.validation.Validator to be injected wherever validation is needed in your application. - Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean: + Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean: ]]> @@ -1279,26 +1279,33 @@ public class Person { The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism. A JSR-303 provider, such as Hibernate Validator, is expected to be present in the classpath and will be detected automatically. + + + Using LocalValidatorFactoryBean programmatically + If you choose to use LocalValidatorFactoryBean + programmatically – i.e., by instantiating it directly – make sure + you call its afterPropertiesSet() method. Otherwise, the + LocalValidatorFactoryBean will not be + initialized properly. + +
Injecting a Validator - LocalValidatorFactoryBean implements both javax.validation.Validator and org.springframework.validation.Validator. + LocalValidatorFactoryBean implements both javax.validation.Validator and org.springframework.validation.Validator. You may inject a reference to one of these two interfaces into beans that need to invoke validation logic. Inject a reference to javax.validation.Validator if you prefer to work with the JSR-303 API directly: - import javax.validation.Validator; @Service public class MyService { @Autowired private Validator validator; - -}]]> - + Inject a reference to org.springframework.validation.Validator if your bean requires the Spring Validation API: @@ -1324,7 +1331,7 @@ public class MyService { At runtime, a ConstraintValidatorFactory instantiates the referenced implementation when the constraint annotation is encountered in your domain model. - By default, the LocalValidatorFactoryBean configures a SpringConstraintValidatorFactory that uses Spring to create ConstraintValidator instances. + By default, the LocalValidatorFactoryBean configures a SpringConstraintValidatorFactory that uses Spring to create ConstraintValidator instances. This allows your custom ConstraintValidators to benefit from dependency injection like any other Spring bean. @@ -1355,9 +1362,9 @@ public class MyConstraintValidator implements ConstraintValidator {
Additional Configuration Options - The default LocalValidatorFactoryBean configuration should prove sufficient for most cases. + The default LocalValidatorFactoryBean configuration should prove sufficient for most cases. There are a number of other configuration options for various JSR-303 constructs, from message interpolation to traversal resolution. - See the JavaDocs of LocalValidatorFactoryBean more information on these options. + See the JavaDocs of LocalValidatorFactoryBean more information on these options.
@@ -1366,25 +1373,24 @@ public class MyConstraintValidator implements ConstraintValidator { Since Spring 3, a DataBinder instance can be configured with a Validator. Once configured, the Validator may be invoked by calling binder.validate(). - Any validation Errors are automatically added to the binder's BindingResults. + Any validation Errors are automatically added to the binder's BindingResult. When working with the DataBinder programatically, this can be used to invoke validation logic after binding to a target object: - Foo target = new Foo(); DataBinder binder = new DataBinder(target); binder.setValidator(new FooValidator()); -// bind to the target object +// bind to the target object binder.bind(propertyValues); -// validate the target object +// validate the target object binder.validate(); -// get BindingResults that include any validation errors -BindingResults results = binder.getBindingResults();}]]> - +// get BindingResult that includes any validation errors +BindingResult results = binder.getBindingResult(); +
Spring MVC 3 Validation @@ -1397,15 +1403,12 @@ BindingResults results = binder.getBindingResults();}]]> To trigger validation of a @Controller input, simply annotate the input argument as @Valid: - @Controller public class MyController { @RequestMapping("/foo", method=RequestMethod.POST) - public void processFoo(@Valid Foo foo) { ... } - -}]]> - + public void processFoo(@Valid Foo foo) { /* ... */ } + Spring MVC will validate a @Valid object after binding so-long as an appropriate Validator has been configured. @@ -1457,7 +1460,7 @@ public class MyController { With JSR-303, the default javax.validation.Validator implementation is generic. A single instance typically coordinates the validation of all application objects that declare validation constraints. - To configure such a general purpose Validator for use by Spring MVC, simply inject a LocalValidatorFactoryBean reference into the WebBindingInitializer. + To configure such a general purpose Validator for use by Spring MVC, simply inject a LocalValidatorFactoryBean reference into the WebBindingInitializer. A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below: @@ -1479,10 +1482,10 @@ public class MyController { With this configuration, anytime a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider. JSR-303, in turn, will enforce any constraints declared against the input. - Any ConstaintViolations will automatically be exposed as BindingResults renderable by standard Spring MVC form tags. + Any ConstaintViolations will automatically be exposed as errors in the BindingResult renderable by standard Spring MVC form tags.
- + \ No newline at end of file