From c58378603a15815c4bda75a9c2af0bf837f358e9 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Thu, 12 Nov 2009 06:22:35 +0000 Subject: [PATCH] validation updateS --- spring-framework-reference/src/validation.xml | 83 ++++++++++--------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 1f1e52373fb..7644de7c417 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -1205,10 +1205,10 @@ public interface FormatterRegistry { You may also define your own custom constraints. - To illustrate, consider a simple Person model with two properties: + To illustrate, consider a simple PersonForm model with two properties: @@ -1216,7 +1216,7 @@ public class Person { JSR-303 allows you to define declarative validation constraints against such properties: - - - 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 @@ -1271,7 +1261,8 @@ public class Person { Inject a reference to javax.validation.Validator if you prefer to work with the JSR-303 API directly: - import javax.validation.Validator; + +import javax.validation.Validator; @Service public class MyService { @@ -1410,44 +1401,56 @@ public class MyController { }]]> Second, you may call setValidator(Validator) on the global WebBindingInitializer. - This allows you to configure a Validator instance across all @Controllers: + This allows you to configure a Validator instance across all @Controllers. + This can be achieved easily by using the Spring MVC namespace: - - - - - - - -]]> + + + + + + +]]> +
Configuring a JSR-303 Validator for use by Spring MVC - 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. + With JSR-303, the default javax.validation.Validator implementation is typically global. + A single instance typically validates all application objects that declare validation constraints. + To configure such a general purpose Validator for use by Spring MVC, simply add a JSR-303 Provider to your classpath. + Spring MVC will detect it and automatically configure JSR-303 for use across all Controllers. - A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below: + The Spring MVC configuration required to configure JSR-303 support is shown below: - - - - - - - - + + - -]]> + + + + +]]> + - With this configuration, anytime a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider. + With this minimal 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 errors in the BindingResult renderable by standard Spring MVC form tags.