polish
This commit is contained in:
parent
d5ed42e860
commit
90697e884d
|
|
@ -1207,6 +1207,7 @@ public interface FormatterRegistry {
|
|||
This applies the Formatter to the field, and overrides any Formatter that would have been applied by field type or annotation.
|
||||
</para>
|
||||
</section>
|
||||
</section>
|
||||
<section id="validation.beanvalidation">
|
||||
<title>Spring 3 Validation</title>
|
||||
<para>
|
||||
|
|
@ -1215,7 +1216,7 @@ public interface FormatterRegistry {
|
|||
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.
|
||||
</para>
|
||||
<secton id="validation.beanvalidation.overview">
|
||||
<section id="validation.beanvalidation.overview">
|
||||
<title>Overview of the Bean Validation API (JSR-303)</title>
|
||||
<para>
|
||||
The Bean Validation API (JSR-303) standardizes validation constraint declaration and metadata for the Java platform.
|
||||
|
|
@ -1244,6 +1245,7 @@ public class Person {
|
|||
|
||||
@Min(0)
|
||||
private int age;
|
||||
|
||||
}]]>
|
||||
</programlisting>
|
||||
<para>
|
||||
|
|
@ -1255,7 +1257,7 @@ public class Person {
|
|||
For how to setup a JSR-303 implementation as a Spring bean, keep reading.
|
||||
</para>
|
||||
</section>
|
||||
<secton id="validation.beanvalidation.spring">
|
||||
<section id="validation.beanvalidation.spring">
|
||||
<title>Configuring a Bean Validation Implementation</title>
|
||||
<para>
|
||||
Spring provides full support for the JSR-303 Bean Validation API.
|
||||
|
|
@ -1266,8 +1268,7 @@ public class Person {
|
|||
Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean:
|
||||
</para>
|
||||
<programlisting language="xml"><![CDATA[
|
||||
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
|
||||
}]]>
|
||||
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />]]>
|
||||
</programlisting>
|
||||
<para>
|
||||
The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism.
|
||||
|
|
@ -1280,7 +1281,7 @@ public class Person {
|
|||
Once created, you may inject a reference to either of these interfaces to other beans that need to invoke JSR-303 validation logic.
|
||||
</para>
|
||||
<para>
|
||||
Inject a reference to <code>javax.validation.Validator</code> if you want 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:
|
||||
</para>
|
||||
<programlisting language="java"><![CDATA[
|
||||
import javax.validation.Validator;
|
||||
|
|
@ -1290,6 +1291,7 @@ public class MyService {
|
|||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
}]]>
|
||||
</programlisting>
|
||||
<para>
|
||||
|
|
@ -1303,6 +1305,7 @@ import org.springframework.validation.Validator;
|
|||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
}]]>
|
||||
</programlisting>
|
||||
</section>
|
||||
|
|
@ -1327,8 +1330,7 @@ import org.springframework.validation.Validator;
|
|||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy=MyConstraintValidator.class)
|
||||
public @interface MyConstraint {
|
||||
}
|
||||
]]>
|
||||
}]]>
|
||||
</programlisting>
|
||||
<programlisting language="java"><![CDATA[
|
||||
import javax.validation.ConstraintValidator;
|
||||
|
|
@ -1350,15 +1352,15 @@ public class MyConstraintValidator implements ConstraintValidator {
|
|||
</para>
|
||||
</section>
|
||||
</section>
|
||||
<secton id="validation.binder">
|
||||
<section id="validation.binder">
|
||||
<title>Configuring a DataBinder</title>
|
||||
<para>
|
||||
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>.
|
||||
Any ValidationErrors are automatically added to the binder's BindingResults.
|
||||
Any validation Errors are automatically added to the binder's BindingResults.
|
||||
</para>
|
||||
<para>
|
||||
When working with the DataBinder programatically, this can be used to invoke validation logic after binding to a target object:
|
||||
When working with the DataBinder programatically, this feature can be used to invoke validation logic after binding to a target object:
|
||||
</para>
|
||||
<programlisting language="java"><![CDATA[
|
||||
DataBinder binder = new DataBinder(new Foo());
|
||||
|
|
@ -1371,11 +1373,10 @@ binder.bind(propertyValues);
|
|||
binder.validate();
|
||||
|
||||
// get BindingResults that include any validation errors
|
||||
BindingResults results = binder.getBindingResults();
|
||||
}]]>
|
||||
BindingResults results = binder.getBindingResults();}]]>
|
||||
</programlisting>
|
||||
</section>
|
||||
<secton id="validation.mvc">
|
||||
<section id="validation.mvc">
|
||||
<title>Spring MVC 3 Validation</title>
|
||||
<para>
|
||||
Spring MVC 3 now has the ability to automatically validate @Controller method arguments after binding.
|
||||
|
|
@ -1392,6 +1393,7 @@ public class MyController {
|
|||
|
||||
@RequestMapping("/foo", method=RequestMethod.POST)
|
||||
public void processFoo(@Valid Foo foo) { ... }
|
||||
|
||||
}]]>
|
||||
</programlisting>
|
||||
<para>
|
||||
|
|
@ -1421,9 +1423,8 @@ public class MyController {
|
|||
|
||||
@RequestMapping("/foo", method=RequestMethod.POST)
|
||||
public void processFoo(@Valid Foo foo) { ... }
|
||||
}
|
||||
|
||||
]]>
|
||||
}]]>
|
||||
</programlisting>
|
||||
<para>
|
||||
Second, you may call setValidator(Validator) on the global WebBindingInitializer.
|
||||
|
|
@ -1472,6 +1473,5 @@ public class MyController {
|
|||
</section>
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
</chapter>
|
||||
|
|
|
|||
Loading…
Reference in New Issue