diff --git a/src/reference/docbook/validation.xml b/src/reference/docbook/validation.xml index 0fa6bc2a4a..913b341628 100644 --- a/src/reference/docbook/validation.xml +++ b/src/reference/docbook/validation.xml @@ -1101,7 +1101,7 @@ public interface ConversionService { ]]> It is also common to use a ConversionService within a Spring MVC - application. See + application. See for details on use with <mvc:annotation-driven/>. @@ -1415,7 +1415,7 @@ public interface FormatterRegistrar { -
+
Configuring Formatting in Spring MVC In a Spring MVC application, you may configure a custom @@ -1498,6 +1498,97 @@ public interface FormatterRegistrar {
+
+ Configuring a global date & time format + + By default, date and time fields that are not annotated with + @DateTimeFormat are converted from strings + using the the DateFormat.SHORT style. If you prefer, + you can change this by defining your own global format. + + You will need to ensure that Spring does not register default + formatters and instead you should register all formatters manually. Use the + org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar + or org.springframework.format.datetime.DateFormatterRegistrar + class depending on your use of the Joda Time library. + + For example, the following Java configuration will register a global + 'yyyyMMdd' format. This example does not depend on the + Joda Time: + @Configurable +public class AppConfig { + + @Bean + public FormattingConversionService conversionService() { + + // Use the DefaultFormattingConversionService but do not register defaults + DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false); + + // Ensure @NumberFormat is still supported + conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); + + // Register date conversion with a specific global format + DateFormatterRegistrar registrar = new DateFormatterRegistrar(); + registrar.setFormatter(new DateFormatter("yyyyMMdd")); + registrar.registerFormatters(conversionService); + + return conversionService; + } +} + + If you prefer XML based configuration you can use a + FormattingConversionServiceFactoryBean. Here is the same + example, this time using Joda Time: + + + + + + + + + + + + + + + + + + + + + +]]> + + + Joda Time provides separate distinct types to represent + date, time and + date-time values. The dateFormatter + , timeFormatter and dateTimeFormatter + properties of the JodaTimeFormatterRegistrar should + be used to configure the different formats for each type. The + DateTimeFormatterFactoryBean provides a + convenient way to create formatters. + + + If you are using Spring MVC remember to explicitly configure the + conversion service that is used. For Java based + @Configuration this means extending the + WebMvcConfigurationSupport class and overriding + the mvcConversionService() method. For XML you should + use the 'conversion-service' attribute of the + mvc:annotation-driven element. See + for details. +
+
Spring 3 Validation