Skip to content

Commit 726a47d

Browse files
committed
Refactor Spring MVC related conversion/validation docs
Conversion and validation documentation related to Spring MVC is now consolidated in the Spring MVC chapter with references to and from the Validation and Data Binding chapter. Examples have been updated to include MVC Java config as well.
1 parent 7cac5d6 commit 726a47d

File tree

2 files changed

+146
-232
lines changed

2 files changed

+146
-232
lines changed

src/asciidoc/core-validation.adoc

Lines changed: 4 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ either of the Converter, ConverterFactory, or GenericConverter interfaces.
957957
----
958958

959959
It is also common to use a ConversionService within a Spring MVC application. See
960-
<<format-configuring-formatting-mvc>> for details on use with `<mvc:annotation-driven/>`.
960+
<<mvc-config-conversion>> in the Spring MVC chapter.
961961

962962
In certain situations you may wish to apply formatting during conversion. See
963963
<<format-FormatterRegistry-SPI>> for details on using
@@ -1308,85 +1308,8 @@ converter and formatter registration.
13081308

13091309
[[format-configuring-formatting-mvc]]
13101310
=== Configuring Formatting in Spring MVC
1311-
In a Spring MVC application, you may configure a custom ConversionService instance
1312-
explicitly as an attribute of the `annotation-driven` element of the MVC namespace. This
1313-
ConversionService will then be used anytime a type conversion is required during
1314-
Controller model binding. If not configured explicitly, Spring MVC will automatically
1315-
register default formatters and converters for common types such as numbers and dates.
13161311

1317-
To rely on default formatting rules, no custom configuration is required in your Spring
1318-
MVC config XML:
1319-
1320-
[source,xml,indent=0]
1321-
[subs="verbatim,quotes"]
1322-
----
1323-
<?xml version="1.0" encoding="UTF-8"?>
1324-
<beans xmlns="http://www.springframework.org/schema/beans"
1325-
xmlns:mvc="http://www.springframework.org/schema/mvc"
1326-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1327-
xsi:schemaLocation="
1328-
http://www.springframework.org/schema/beans
1329-
http://www.springframework.org/schema/beans/spring-beans.xsd
1330-
http://www.springframework.org/schema/mvc
1331-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
1332-
1333-
<mvc:annotation-driven/>
1334-
1335-
</beans>
1336-
----
1337-
1338-
With this one-line of configuration, default formatters for Numbers and Date types will
1339-
be installed, including support for the @NumberFormat and @DateTimeFormat annotations.
1340-
Full support for the Joda Time formatting library is also installed if Joda Time is
1341-
present on the classpath.
1342-
1343-
To inject a ConversionService instance with custom formatters and converters registered,
1344-
set the conversion-service attribute and then specify custom converters, formatters, or
1345-
FormatterRegistrars as properties of the FormattingConversionServiceFactoryBean:
1346-
1347-
[source,xml,indent=0]
1348-
[subs="verbatim,quotes"]
1349-
----
1350-
<?xml version="1.0" encoding="UTF-8"?>
1351-
<beans xmlns="http://www.springframework.org/schema/beans"
1352-
xmlns:mvc="http://www.springframework.org/schema/mvc"
1353-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1354-
xsi:schemaLocation="
1355-
http://www.springframework.org/schema/beans
1356-
http://www.springframework.org/schema/beans/spring-beans.xsd
1357-
http://www.springframework.org/schema/mvc
1358-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
1359-
1360-
<mvc:annotation-driven conversion-service="conversionService"/>
1361-
1362-
<bean id="conversionService"
1363-
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
1364-
<property name="converters">
1365-
<set>
1366-
<bean class="org.example.MyConverter"/>
1367-
</set>
1368-
</property>
1369-
<property name="formatters">
1370-
<set>
1371-
<bean class="org.example.MyFormatter"/>
1372-
<bean class="org.example.MyAnnotationFormatterFactory"/>
1373-
</set>
1374-
</property>
1375-
<property name="formatterRegistrars">
1376-
<set>
1377-
<bean class="org.example.MyFormatterRegistrar"/>
1378-
</set>
1379-
</property>
1380-
</bean>
1381-
1382-
</beans>
1383-
----
1384-
1385-
[NOTE]
1386-
====
1387-
See <<format-FormatterRegistrar-SPI>> and the `FormattingConversionServiceFactoryBean`
1388-
for more information on when to use FormatterRegistrars.
1389-
====
1312+
See <<mvc-config-conversion>> in the Spring MVC chapter.
13901313

13911314

13921315

@@ -1479,7 +1402,7 @@ If you are using Spring MVC remember to explicitly configure the conversion serv
14791402
is used. For Java based `@Configuration` this means extending the
14801403
`WebMvcConfigurationSupport` class and overriding the `mvcConversionService()` method.
14811404
For XML you should use the `'conversion-service'` attribute of the
1482-
`mvc:annotation-driven` element. See <<format-configuring-formatting-mvc>> for details.
1405+
`mvc:annotation-driven` element. See <<mvc-config-conversion>> for details.
14831406

14841407

14851408

@@ -1707,131 +1630,6 @@ locally on a DataBinder instance. See <<validation-mvc-configuring>>.
17071630

17081631
[[validation-mvc]]
17091632
=== Spring MVC 3 Validation
1710-
Beginning with Spring 3, Spring MVC has the ability to automatically validate
1711-
`@Controller` inputs. In previous versions it was up to the developer to manually invoke
1712-
validation logic.
1713-
1714-
1715-
[[validation-mvc-triggering]]
1716-
==== Triggering @Controller Input Validation
1717-
To trigger validation of a `@Controller` input, simply annotate the input argument as
1718-
++@Valid++:
1719-
1720-
[source,java,indent=0]
1721-
[subs="verbatim,quotes"]
1722-
----
1723-
@Controller
1724-
public class MyController {
1725-
1726-
@RequestMapping(path="/foo", method=RequestMethod.POST)
1727-
public void processFoo(**@Valid** Foo foo) { /* ... */ }
1728-
----
1729-
1730-
Spring MVC will validate a @Valid object after binding so-long as an appropriate
1731-
Validator has been configured.
1732-
1733-
[NOTE]
1734-
====
1735-
The @Valid annotation is part of the standard JSR-303 Bean Validation API, and is not a
1736-
Spring-specific construct.
1737-
====
1738-
1739-
1740-
[[validation-mvc-configuring]]
1741-
==== Configuring a Validator for use by Spring MVC
1742-
The `Validator` instance invoked when a `@Valid` method argument is encountered may be
1743-
configured in two ways. First, you may call `binder.setValidator(Validator)` within a
1744-
++@Controller++'s `@InitBinder` callback. This allows you to configure a `Validator`
1745-
instance per `@Controller` class:
1746-
1747-
[source,java,indent=0]
1748-
[subs="verbatim,quotes"]
1749-
----
1750-
@Controller
1751-
public class MyController {
1752-
1753-
@InitBinder
1754-
protected void initBinder(WebDataBinder binder) {
1755-
binder.setValidator(new FooValidator());
1756-
}
1757-
1758-
@RequestMapping(path="/foo", method=RequestMethod.POST)
1759-
public void processFoo(@Valid Foo foo) { ... }
1760-
1761-
}
1762-
----
1763-
1764-
Second, you may call `setValidator(Validator)` on the global `WebBindingInitializer`. This
1765-
allows you to configure a `Validator` instance across all `@Controller` classes. This can be
1766-
achieved easily by using the Spring MVC namespace:
1767-
1768-
[source,xml,indent=0]
1769-
[subs="verbatim,quotes"]
1770-
----
1771-
<?xml version="1.0" encoding="UTF-8"?>
1772-
<beans xmlns="http://www.springframework.org/schema/beans"
1773-
xmlns:mvc="http://www.springframework.org/schema/mvc"
1774-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1775-
xsi:schemaLocation="
1776-
http://www.springframework.org/schema/beans
1777-
http://www.springframework.org/schema/beans/spring-beans.xsd
1778-
http://www.springframework.org/schema/mvc
1779-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
1780-
1781-
<mvc:annotation-driven validator="globalValidator"/>
1782-
1783-
</beans>
1784-
----
1785-
1786-
To combine a global and a local validator, configure the global validator as shown above
1787-
and then add a local validator:
1788-
1789-
[source,java,indent=0]
1790-
[subs="verbatim,quotes"]
1791-
----
1792-
@Controller
1793-
public class MyController {
1794-
1795-
@InitBinder
1796-
protected void initBinder(WebDataBinder binder) {
1797-
binder.addValidators(new FooValidator());
1798-
}
1799-
1800-
}
1801-
----
1802-
1803-
1804-
[[validation-mvc-jsr303]]
1805-
==== Configuring a JSR-303/JSR-349 Validator for use by Spring MVC
1806-
With Bean Validation, a single `javax.validation.Validator` instance typically validates
1807-
__all__ model objects that declare validation constraints. To configure such a JSR-303
1808-
backed Validator with Spring MVC, simply add a Bean Validation provider, such as
1809-
Hibernate Validator, to your classpath. Spring MVC will detect it and automatically
1810-
enable Bean Validation support across all Controllers.
1811-
1812-
The Spring MVC configuration required to enable Bean Validation support is shown below:
1813-
1814-
[source,xml,indent=0]
1815-
[subs="verbatim,quotes"]
1816-
----
1817-
<?xml version="1.0" encoding="UTF-8"?>
1818-
<beans xmlns="http://www.springframework.org/schema/beans"
1819-
xmlns:mvc="http://www.springframework.org/schema/mvc"
1820-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1821-
xsi:schemaLocation="
1822-
http://www.springframework.org/schema/beans
1823-
http://www.springframework.org/schema/beans/spring-beans.xsd
1824-
http://www.springframework.org/schema/mvc
1825-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
1826-
1827-
<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
1828-
<mvc:annotation-driven/>
1829-
1830-
</beans>
1831-
----
18321633

1833-
With this minimal configuration, anytime a `@Valid` `@Controller` input is encountered, it
1834-
will be validated by the Bean Validation provider. That provider, in turn, will enforce
1835-
any constraints declared against the input. Any ++ConstraintViolation++s will automatically
1836-
be exposed as errors in the `BindingResult` renderable by standard Spring MVC form tags.
1634+
See <<mvc-config-validation>> in the Spring MVC chapter.
18371635

0 commit comments

Comments
 (0)