polish
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1884 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
af0f00839e
commit
a29bc9012a
|
|
@ -1278,7 +1278,7 @@ public class Person {
|
||||||
<title>Injecting a Validator</title>
|
<title>Injecting a Validator</title>
|
||||||
<para>
|
<para>
|
||||||
LocalValidatorFactoryBean implements both <code>javax.validation.Validator</code> and <code>org.springframework.validation.Validator</code>.
|
LocalValidatorFactoryBean implements both <code>javax.validation.Validator</code> and <code>org.springframework.validation.Validator</code>.
|
||||||
Inject a reference to one of these two interfaces into beans that need to invoke validation logic.
|
You may inject a reference to one of these two interfaces into beans that need to invoke validation logic.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
Inject a reference to <code>javax.validation.Validator</code> if you prefer to work with the JSR-303 API directly:
|
Inject a reference to <code>javax.validation.Validator</code> if you prefer to work with the JSR-303 API directly:
|
||||||
|
|
@ -1295,7 +1295,7 @@ public class MyService {
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
Inject a reference to <code>org.springframework.validation.Validator</code> if your bean depends on the existing Spring Validation API:
|
Inject a reference to <code>org.springframework.validation.Validator</code> if your bean requires the Spring Validation API:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
|
|
@ -1315,15 +1315,15 @@ public class MyService {
|
||||||
Each JSR-303 validation constraint consists of two parts.
|
Each JSR-303 validation constraint consists of two parts.
|
||||||
First, a @Constraint annotation that declares the constraint and its configurable properties.
|
First, a @Constraint annotation that declares the constraint and its configurable properties.
|
||||||
Second, an implementation of the <code>javax.validation.ConstraintValidator</code> interface that implements the constraint's behavior.
|
Second, an implementation of the <code>javax.validation.ConstraintValidator</code> interface that implements the constraint's behavior.
|
||||||
To associate a declaration with an implementation, each @Constraint annotation references its corresponding ValidationConstraint implementation class.
|
To associate a declaration with an implementation, each @Constraint annotation references a corresponding ValidationConstraint implementation class.
|
||||||
At runtime, the <code>ConstraintValidatorFactory</code> then creates instances of this class when the constraint annotation is encountered in your domain model.
|
At runtime, a <code>ConstraintValidatorFactory</code> instantiates the referenced implementation when the constraint annotation is encountered in your domain model.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
The <code>LocalValidatorFactoryBean</code> automatically configures a <code>SpringConstraintValidatorFactory</code> that uses Spring to create ConstraintValidator instances.
|
By default, the <code>LocalValidatorFactoryBean</code> configures a <code>SpringConstraintValidatorFactory</code> that uses Spring to create ConstraintValidator instances.
|
||||||
This allows your custom ConstraintValidators to benefit from dependency injection like any other Spring bean.
|
This allows your custom ConstraintValidators to benefit from dependency injection like any other Spring bean.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
Below is an example of a custom constraint declaration and implementation that uses Spring dependency injection:
|
Shown below is an example of a custom Constraint declaration, followed by an associated <code>ConstraintValidator</code> implementation that uses Spring for dependency injection:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
@Target({ElementType.METHOD, ElementType.FIELD})
|
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||||
|
|
@ -1344,14 +1344,14 @@ public class MyConstraintValidator implements ConstraintValidator {
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
As you can see, the ConstraintValidator implementation above can have its dependencies @Autowired by Spring like any other bean.
|
As you can see, a ConstraintValidator implementation may have its dependencies @Autowired like any other Spring bean.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
<section id="validation.beanvalidation.spring.other">
|
<section id="validation.beanvalidation.spring.other">
|
||||||
<title>Additional Configuration Options</title>
|
<title>Additional Configuration Options</title>
|
||||||
<para>
|
<para>
|
||||||
The default <code>LocalValidatorFactoryBean</code> configuration should prove sufficient for most cases.
|
The default <code>LocalValidatorFactoryBean</code> 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.
|
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.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -1360,20 +1360,21 @@ public class MyConstraintValidator implements ConstraintValidator {
|
||||||
<title>Configuring a DataBinder</title>
|
<title>Configuring a DataBinder</title>
|
||||||
<para>
|
<para>
|
||||||
Since Spring 3, a DataBinder instance can now be configured with a Validator.
|
Since Spring 3, a DataBinder instance can now be configured with a Validator.
|
||||||
Once configured, the Validator may be subsequently invoked by calling <code>binder.validate()</code>.
|
Once configured, the Validator may be invoked by calling <code>binder.validate()</code>.
|
||||||
Any validation Errors are automatically added to the binder's BindingResults.
|
Any validation Errors are automatically added to the binder's BindingResults.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
When working with the DataBinder programatically, this feature can be used to invoke validation logic after binding to a target object:
|
When working with the DataBinder programatically, this can be used to invoke validation logic after binding to a target object:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
DataBinder binder = new DataBinder(new Foo());
|
Foo target = new Foo();
|
||||||
|
DataBinder binder = new DataBinder(target);
|
||||||
binder.setValidator(new FooValidator());
|
binder.setValidator(new FooValidator());
|
||||||
|
|
||||||
// bind to Foo
|
// bind to the target object
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
|
|
||||||
// validate Foo
|
// validate the target object
|
||||||
binder.validate();
|
binder.validate();
|
||||||
|
|
||||||
// get BindingResults that include any validation errors
|
// get BindingResults that include any validation errors
|
||||||
|
|
@ -1449,10 +1450,10 @@ public class MyController {
|
||||||
<section id="validation.mvc.jsr303">
|
<section id="validation.mvc.jsr303">
|
||||||
<title>Configuring a JSR-303 Validator for use by Spring MVC</title>
|
<title>Configuring a JSR-303 Validator for use by Spring MVC</title>
|
||||||
<para>
|
<para>
|
||||||
With JSR-303, the default <code>javax.validation.Validator</code> implementation is quite generic.
|
With JSR-303, the default <code>javax.validation.Validator</code> implementation is generic.
|
||||||
A single instance typically coordinates the validation of <emphasis>all</emphasis> application objects that declare validation constraints.
|
A single instance typically coordinates the validation of <emphasis>all</emphasis> application objects that declare validation constraints.
|
||||||
To configure such a Validator for use by Spring MVC, simply inject a <code>LocalValidatorFactoryBean</code> reference into the <code>WebBindingInitializer</code>.
|
To configure such a general purpose Validator for use by Spring MVC, simply inject a <code>LocalValidatorFactoryBean</code> reference into the <code>WebBindingInitializer</code>.
|
||||||
<code>LocalValidatorFactoryBean</code> already implements <code>org.springframework.validation.Validation</code>, delegating to the JSR-303 provider underneath.
|
<code>LocalValidatorFactoryBean</code> already implements <code>org.springframework.validation.Validator</code>, and delegates to the JSR-303 provider underneath.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below:
|
A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below:
|
||||||
|
|
@ -1472,7 +1473,9 @@ public class MyController {
|
||||||
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />]]>
|
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
With this configuration, anytime a @Valid @Controller method argument is encountered, it will be validated using the JSR-303 provider.
|
With this configuration, anytime a @Valid @Controller method argument is encountered, it will be validated by the JSR-303 provider.
|
||||||
|
JSR-303, in turn, will enforce any constraints declared against the argument.
|
||||||
|
Any ConstaintViolations will automatically be exposed as BindingResults renderable by standard Spring MVC form tags.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue