diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 93840da2771..dc419210db5 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -1207,6 +1207,271 @@ public interface FormatterRegistry { This applies the Formatter to the field, and overrides any Formatter that would have been applied by field type or annotation. +
+ Spring 3 Validation + + Spring 3 introduces several enhancements to its validation support. + First, the JSR-303 Bean Validation API is now fully supported. + Second, when used programatically, Spring's DataBinder can now validate objects as well as bind to them. + Third, Spring MVC now has support for declaratively validating @Controller inputs. + + + Overview of the Bean Validation API (JSR-303) + + The Bean Validation API (JSR-303) standardizes validation constraint declaration and metadata for the Java platform. + Using this API, you annotate domain model properties with declarative validation constraints and the runtime enforces them. + There are a number of built-in constraints you can can take advantage of. + You may also define your own custom constraints. + + + To illustrate, consider a simple Person model with two properties: + + + + + JSR-303 allows you to define declarative validation constraints against such properties: + + + + + When this object is processed by a JSR-303 Validator, these constraints will be validated. + + + For general information on JSR-303, see the Bean Validation Specification. + For information on the specific capabilities of the default reference implementation, see the Hibernate Validator documentation. + For how to setup a JSR-303 implementation as a Spring bean, keep reading. + +
+ + Configuring a Bean Validation Implementation + + Spring provides full support for the JSR-303 Bean Validation API. + This includes convenient support for bootstrapping a JSR-303 implementation as a Spring bean. + This allows a javax.validation.Validator to be injected wherever validation is needed. + + + Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean: + + +}]]> + + + The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism. + The JSR-303 provider, such as Hibernate Validator, will be detected in your classpath automatically. + +
+ Injecting a Validator + + LocalValidatorFactoryBean implements javax.validation.Validator as well as org.springframework.validation.Validator. + Once created, you may inject a reference to either of these interfaces to other beans that need to invoke JSR-303 validation logic. + + + Inject a reference to javax.validation.Validator if you want to work with the JSR-303 API directly: + + + + + Inject a reference to org.springframework.validation.Validator if you prefer to work with the familiar Spring Validation API: + + + +
+
+ Configuring Custom Constraints + + Each JSR-303 validation constraint consists of two parts. + First, a @Constraint annotation that declares the constraint and its configurable properties. + Second, an implementation of the javax.validation.ConstraintValidator interface that implements the constraint's behavior. + To associate a declaration with an implementation, each @Constraint annotation references its corresponding ValidationConstraint implementation class. + At runtime, the ConstraintValidatorFactory then creates instances of this class when the constraint annotation is encountered in your domain model. + + + The LocalValidatorFactoryBean automatically 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. + + + Below is an example of a custom constraint declaration and implementation that uses Spring dependency injection: + + + + + +
+
+ Additional Configuration Options + + The default LocalValidatorFactoryBean configuration should prove sufficient for most cases. + There are a number of other explicit configuration options for various JSR-303 constructs, from message interpolation to traversal resolution. + See the JavaDocs of LocalValidatorFactoryBean more information on these options. + +
+ + + Configuring a DataBinder + + Since Spring 3, a DataBinder instance can now be configured with a Validator. + Once configured, the Validator may be subsequently invoked by calling binder.validate(). + Any ValidationErrors are automatically added to the binder's BindingResults. + + + When working with the DataBinder programatically, this can be used to invoke validation logic after binding to a target object: + + + + + + Spring MVC 3 Validation + + Spring MVC 3 now has the ability to automatically validate @Controller method arguments after binding. + Before version 3, it was up to the developer to manually invoke object Validation logic. + +
+ Triggering @Controller Method Argument Validation + + To trigger validation of a @Controller method argument, simply annotate the argument as @Valid: + + + + + Spring MVC will validate a @Valid object after binding so-long as an appropriate Validator has been configured. + + + + The @Valid annotation is part of the standard JSR-303 Bean Validation API, and not a Spring-specific construct. + + +
+
+ Configuring a Validator for use by Spring MVC + + The Validator instance invoked when a @Valid method argument is encountered may be configured in two ways. + First, you may call binder.setValidator(Validator) within a @Controller's @InitBinder callback method. + This allows you to configure a Validator instance per @Controller class: + + + + + Second, you may call setValidator(Validator) on the global WebBindingInitializer. + This allows you to configure a Validator instance across all @Controllers: + + + + + + + + + +]]> + +
+
+ Configuring a JSR-303 Validator for use by Spring MVC + + With JSR-303, the default javax.validation.Validator implementation is quite generic. + A single instance typically coordinates the validation of all application objects that declare validation constraints. + To configure such a Validator for use by Spring MVC, simply inject a LocalValidatorFactoryBean reference into the WebBindingInitializer as shown in the previous section. + LocalValidatorFactoryBean already implements org.springframework.validation.Validation, delegating to the JSR-303 provider underneath. + + + A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below: + + + + + + + + + + + + +]]> + + + With this configuration, anytime a @Valid @Controller method argument is encountered, it will be validated using JSR-303. + +
+ +