Skip to content

Commit ab10d37

Browse files
committed
Corrections regarding BindingResult(s); added a tip regarding programmatic use of LocalValidatorFactoryBean.
1 parent 710ae3a commit ab10d37

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

spring-framework-reference/src/validation.xml

+30-27
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ public class Person {
12701270
This allows a <code>javax.validation.Validator</code> to be injected wherever validation is needed in your application.
12711271
</para>
12721272
<para>
1273-
Use the LocalValidatorFactoryBean to configure a default JSR-303 Validator as a Spring bean:
1273+
Use the <classname>LocalValidatorFactoryBean</classname> to configure a default JSR-303 Validator as a Spring bean:
12741274
</para>
12751275
<programlisting language="xml"><![CDATA[
12761276
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />]]>
@@ -1279,26 +1279,33 @@ public class Person {
12791279
The basic configuration above will trigger JSR-303 to initialize using its default bootstrap mechanism.
12801280
A JSR-303 provider, such as Hibernate Validator, is expected to be present in the classpath and will be detected automatically.
12811281
</para>
1282+
1283+
<tip>
1284+
<title>Using LocalValidatorFactoryBean programmatically</title>
1285+
<para>If you choose to use <classname>LocalValidatorFactoryBean</classname>
1286+
programmatically – i.e., by instantiating it directly – make sure
1287+
you call its <literal>afterPropertiesSet()</literal> method. Otherwise, the
1288+
<classname>LocalValidatorFactoryBean</classname> will not be
1289+
initialized properly.</para>
1290+
</tip>
1291+
12821292
<section id="validation.beanvalidation.spring.inject">
12831293
<title>Injecting a Validator</title>
12841294
<para>
1285-
LocalValidatorFactoryBean implements both <code>javax.validation.Validator</code> and <code>org.springframework.validation.Validator</code>.
1295+
<classname>LocalValidatorFactoryBean</classname> implements both <code>javax.validation.Validator</code> and <code>org.springframework.validation.Validator</code>.
12861296
You may inject a reference to one of these two interfaces into beans that need to invoke validation logic.
12871297
</para>
12881298
<para>
12891299
Inject a reference to <code>javax.validation.Validator</code> if you prefer to work with the JSR-303 API directly:
12901300
</para>
1291-
<programlisting language="java"><![CDATA[
1292-
import javax.validation.Validator;
1301+
<programlisting language="java">import javax.validation.Validator;
12931302

12941303
@Service
12951304
public class MyService {
12961305

12971306
@Autowired
12981307
private Validator validator;
1299-
1300-
}]]>
1301-
</programlisting>
1308+
</programlisting>
13021309
<para>
13031310
Inject a reference to <code>org.springframework.validation.Validator</code> if your bean requires the Spring Validation API:
13041311
</para>
@@ -1324,7 +1331,7 @@ public class MyService {
13241331
At runtime, a <code>ConstraintValidatorFactory</code> instantiates the referenced implementation when the constraint annotation is encountered in your domain model.
13251332
</para>
13261333
<para>
1327-
By default, the <code>LocalValidatorFactoryBean</code> configures a <code>SpringConstraintValidatorFactory</code> that uses Spring to create ConstraintValidator instances.
1334+
By default, the <classname>LocalValidatorFactoryBean</classname> configures a <code>SpringConstraintValidatorFactory</code> that uses Spring to create ConstraintValidator instances.
13281335
This allows your custom ConstraintValidators to benefit from dependency injection like any other Spring bean.
13291336
</para>
13301337
<para>
@@ -1355,9 +1362,9 @@ public class MyConstraintValidator implements ConstraintValidator {
13551362
<section id="validation.beanvalidation.spring.other">
13561363
<title>Additional Configuration Options</title>
13571364
<para>
1358-
The default <code>LocalValidatorFactoryBean</code> configuration should prove sufficient for most cases.
1365+
The default <classname>LocalValidatorFactoryBean</classname> configuration should prove sufficient for most cases.
13591366
There are a number of other configuration options for various JSR-303 constructs, from message interpolation to traversal resolution.
1360-
See the JavaDocs of LocalValidatorFactoryBean more information on these options.
1367+
See the JavaDocs of <classname>LocalValidatorFactoryBean</classname> more information on these options.
13611368
</para>
13621369
</section>
13631370
</section>
@@ -1366,25 +1373,24 @@ public class MyConstraintValidator implements ConstraintValidator {
13661373
<para>
13671374
Since Spring 3, a DataBinder instance can be configured with a Validator.
13681375
Once configured, the Validator may be invoked by calling <code>binder.validate()</code>.
1369-
Any validation Errors are automatically added to the binder's BindingResults.
1376+
Any validation Errors are automatically added to the binder's BindingResult.
13701377
</para>
13711378
<para>
13721379
When working with the DataBinder programatically, this can be used to invoke validation logic after binding to a target object:
13731380
</para>
1374-
<programlisting language="java"><![CDATA[
1375-
Foo target = new Foo();
1381+
<programlisting language="java">Foo target = new Foo();
13761382
DataBinder binder = new DataBinder(target);
13771383
binder.setValidator(new FooValidator());
13781384

1379-
// bind to the target object
1385+
<lineannotation>// bind to the target object</lineannotation>
13801386
binder.bind(propertyValues);
13811387

1382-
// validate the target object
1388+
<lineannotation>// validate the target object</lineannotation>
13831389
binder.validate();
13841390

1385-
// get BindingResults that include any validation errors
1386-
BindingResults results = binder.getBindingResults();}]]>
1387-
</programlisting>
1391+
<lineannotation>// get BindingResult that includes any validation errors</lineannotation>
1392+
BindingResult results = binder.getBindingResult();
1393+
</programlisting>
13881394
</section>
13891395
<section id="validation.mvc">
13901396
<title>Spring MVC 3 Validation</title>
@@ -1397,15 +1403,12 @@ BindingResults results = binder.getBindingResults();}]]>
13971403
<para>
13981404
To trigger validation of a @Controller input, simply annotate the input argument as @Valid:
13991405
</para>
1400-
<programlisting language="java"><![CDATA[
1401-
@Controller
1406+
<programlisting language="java">@Controller
14021407
public class MyController {
14031408

14041409
@RequestMapping("/foo", method=RequestMethod.POST)
1405-
public void processFoo(@Valid Foo foo) { ... }
1406-
1407-
}]]>
1408-
</programlisting>
1410+
public void processFoo(<emphasis role="bold">@Valid</emphasis> Foo foo) { <lineannotation>/* ... */</lineannotation> }
1411+
</programlisting>
14091412
<para>
14101413
Spring MVC will validate a @Valid object after binding so-long as an appropriate Validator has been configured.
14111414
</para>
@@ -1457,7 +1460,7 @@ public class MyController {
14571460
<para>
14581461
With JSR-303, the default <code>javax.validation.Validator</code> implementation is generic.
14591462
A single instance typically coordinates the validation of <emphasis>all</emphasis> application objects that declare validation constraints.
1460-
To configure such a general purpose Validator for use by Spring MVC, simply inject a <code>LocalValidatorFactoryBean</code> reference into the <code>WebBindingInitializer</code>.
1463+
To configure such a general purpose Validator for use by Spring MVC, simply inject a <classname>LocalValidatorFactoryBean</classname> reference into the <code>WebBindingInitializer</code>.
14611464
</para>
14621465
<para>
14631466
A full configuration example showing injection of a JSR-303 backed Validator into Spring MVC is shown below:
@@ -1479,10 +1482,10 @@ public class MyController {
14791482
<para>
14801483
With this configuration, anytime a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider.
14811484
JSR-303, in turn, will enforce any constraints declared against the input.
1482-
Any ConstaintViolations will automatically be exposed as BindingResults renderable by standard Spring MVC form tags.
1485+
Any ConstaintViolations will automatically be exposed as errors in the BindingResult renderable by standard Spring MVC form tags.
14831486
</para>
14841487
</section>
14851488
</section>
14861489
</section>
14871490

1488-
</chapter>
1491+
</chapter>

0 commit comments

Comments
 (0)