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.
|
This applies the Formatter to the field, and overrides any Formatter that would have been applied by field type or annotation.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
</section>
|
||||||
<section id="validation.beanvalidation">
|
<section id="validation.beanvalidation">
|
||||||
<title>Spring 3 Validation</title>
|
<title>Spring 3 Validation</title>
|
||||||
<para>
|
<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.
|
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.
|
Third, Spring MVC now has support for declaratively validating @Controller inputs.
|
||||||
</para>
|
</para>
|
||||||
<secton id="validation.beanvalidation.overview">
|
<section id="validation.beanvalidation.overview">
|
||||||
<title>Overview of the Bean Validation API (JSR-303)</title>
|
<title>Overview of the Bean Validation API (JSR-303)</title>
|
||||||
<para>
|
<para>
|
||||||
The Bean Validation API (JSR-303) standardizes validation constraint declaration and metadata for the Java platform.
|
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)
|
@Min(0)
|
||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
|
|
@ -1255,7 +1257,7 @@ public class Person {
|
||||||
For how to setup a JSR-303 implementation as a Spring bean, keep reading.
|
For how to setup a JSR-303 implementation as a Spring bean, keep reading.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
<secton id="validation.beanvalidation.spring">
|
<section id="validation.beanvalidation.spring">
|
||||||
<title>Configuring a Bean Validation Implementation</title>
|
<title>Configuring a Bean Validation Implementation</title>
|
||||||
<para>
|
<para>
|
||||||
Spring provides full support for the JSR-303 Bean Validation API.
|
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:
|
Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="xml"><![CDATA[
|
<programlisting language="xml"><![CDATA[
|
||||||
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
|
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />]]>
|
||||||
}]]>
|
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism.
|
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.
|
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>
|
||||||
<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>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
|
|
@ -1290,6 +1291,7 @@ public class MyService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Validator validator;
|
private Validator validator;
|
||||||
|
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
|
|
@ -1298,11 +1300,12 @@ public class MyService {
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MyService {
|
public class MyService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Validator validator;
|
private Validator validator;
|
||||||
|
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -1327,8 +1330,7 @@ import org.springframework.validation.Validator;
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Constraint(validatedBy=MyConstraintValidator.class)
|
@Constraint(validatedBy=MyConstraintValidator.class)
|
||||||
public @interface MyConstraint {
|
public @interface MyConstraint {
|
||||||
}
|
}]]>
|
||||||
]]>
|
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
import javax.validation.ConstraintValidator;
|
import javax.validation.ConstraintValidator;
|
||||||
|
|
@ -1350,15 +1352,15 @@ public class MyConstraintValidator implements ConstraintValidator {
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
<secton id="validation.binder">
|
<section id="validation.binder">
|
||||||
<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 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>
|
||||||
<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>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
DataBinder binder = new DataBinder(new Foo());
|
DataBinder binder = new DataBinder(new Foo());
|
||||||
|
|
@ -1371,11 +1373,10 @@ binder.bind(propertyValues);
|
||||||
binder.validate();
|
binder.validate();
|
||||||
|
|
||||||
// get BindingResults that include any validation errors
|
// get BindingResults that include any validation errors
|
||||||
BindingResults results = binder.getBindingResults();
|
BindingResults results = binder.getBindingResults();}]]>
|
||||||
}]]>
|
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</section>
|
</section>
|
||||||
<secton id="validation.mvc">
|
<section id="validation.mvc">
|
||||||
<title>Spring MVC 3 Validation</title>
|
<title>Spring MVC 3 Validation</title>
|
||||||
<para>
|
<para>
|
||||||
Spring MVC 3 now has the ability to automatically validate @Controller method arguments after binding.
|
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)
|
@RequestMapping("/foo", method=RequestMethod.POST)
|
||||||
public void processFoo(@Valid Foo foo) { ... }
|
public void processFoo(@Valid Foo foo) { ... }
|
||||||
|
|
||||||
}]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
|
|
@ -1421,9 +1423,8 @@ public class MyController {
|
||||||
|
|
||||||
@RequestMapping("/foo", method=RequestMethod.POST)
|
@RequestMapping("/foo", method=RequestMethod.POST)
|
||||||
public void processFoo(@Valid Foo foo) { ... }
|
public void processFoo(@Valid Foo foo) { ... }
|
||||||
}
|
|
||||||
|
|
||||||
]]>
|
}]]>
|
||||||
</programlisting>
|
</programlisting>
|
||||||
<para>
|
<para>
|
||||||
Second, you may call setValidator(Validator) on the global WebBindingInitializer.
|
Second, you may call setValidator(Validator) on the global WebBindingInitializer.
|
||||||
|
|
@ -1472,6 +1473,5 @@ public class MyController {
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
|
||||||
|
|
||||||
</chapter>
|
</chapter>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue