diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 938df2038c..36ab41aec4 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -912,26 +912,28 @@ public class MyService {
Spring 3 Field Formatting - core.convert is a general-purpose type conversion system. - It provides a strongly-typed Converter SPI for implementing conversion logic from one type to another and is not limited to just converting Strings. - As discussed in the previous section, a Spring Container can be configured to use this system to bind bean property values. - In addition, both the Spring Expression Language (SpEL) and DataBinder can use this system to bind values. + As discussed in the previous section, core.convert is a general-purpose type conversion system. + It provides a unified ConversionService API as well as a strongly-typed Converter SPI for implementing conversion logic from one type to another. + A Spring Container uses this system to bind bean property values. + In addition, both the Spring Expression Language (SpEL) and DataBinder use this system to bind field values. For example, when SpEL needs to coerce a Short to a Long to complete an expression.setValue(Object bean, Object value) attempt, the core.convert system performs the coercion. Now consider the type conversion requirements of a typical client environment such as a web or desktop application. In such environments, you typically convert from String to support the client postback process, as well as back to String to support the view rendering process. - The more general core.convert Converter SPI does not address this scenario directly. - To directly address this, Spring 3 introduces a convenient format SPI that provides a simple and robust alternative to PropertyEditors for client environments. + In addition, you often need to localize String values. + The more general core.convert Converter SPI does not address such formatting requirements directly. + To directly address them, Spring 3 introduces a convenient Formatter SPI that provides a simple and robust alternative to PropertyEditors for client environments. - In general, use the Converter SPI when you need to implement general-purpose type conversion logic. - Use Formatters when you're working in a client environment, such as a web application, and need to parse and print localized field values. + In general, use the Converter SPI when you need to implement general-purpose type conversion logic; for example, for converting between a java.util.Date and and java.lang.Long. + Use the Formatter SPI when you're working in a client environment, such as a web application, and need to parse and print localized field values. + The ConversionService provides a unified type conversion API for both SPIs.
Formatter SPI - The org.springframework.format SPI to implement field formatting logic is simple and strongly typed: + The Formatter SPI to implement field formatting logic is simple and strongly typed: {
Annotation-driven Formatting - Field formatting can be triggered by field type or annotation. - To bind an annotation to a formatter, implement AnnotationFormatterFactory: + As you will see, field formatting can be configured by field type or annotation. + To bind an Annotation to a formatter, implement AnnotationFormatterFactory: { Parameterize A to be the field annotationType you wish to associate formatting logic with, for example org.springframework.format.annotation.DateTimeFormat. - Implement the getFieldTypes operation return the types of fields the annotation may be used on. - Implement the getPrinter operation to return the Printer to print the value of an annotated field. - Implement the getParser operation to return the Parser to parse the value of an annotated field. + Have getFieldTypes return the types of fields the annotation may be used on. + Have getPrinter return a Printer to print the value of an annotated field. + Have getParser return a Parser to parse a clientValue for an annotated field. - The example AnnotationFormatterFactory implementation below binds a @NumberFormat Annotation to a formatter. - This annotation allows a number style or pattern to be specified: + The example AnnotationFormatterFactory implementation below binds the @NumberFormat Annotation to a formatter. + This annotation allows either a number style or pattern to be specified: { @@ -1069,7 +1071,7 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF }]]> - Then, to trigger formatting, annotate fields with @NumberFormat: + To trigger formatting, simply annotate fields with @NumberFormat: Format Annotation API A portable format annotation API exists in the org.springframework.format.annotation package. - Use the @NumberFormat to apply formatting to java.lang.Number fields. - Use the @DateTimeFormat to apply formatting to java.util.Date, java.util.Calendar, java.util.Long, or Joda Time fields. + Use @NumberFormat to format java.lang.Number fields. + Use @DateTimeFormat to format java.util.Date, java.util.Calendar, java.util.Long, or Joda Time fields. - The example below shows use of the DateTimeFormat annotation to format a java.util.Date as a ISO Date (yyyy-MM-dd): + The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date (yyyy-MM-dd): FormatterRegistry SPI At runtime, Formatters are registered in a FormatterRegistry. - A FormatterRegistry allows you to configure Formatting rules centrally, instead of duplicating such configuration across your Controllers. + The FormatterRegistry SPI allows you to configure Formatting rules centrally, instead of duplicating such configuration across your Controllers. For example, you might want to enforce that all Date fields are formatted a certain way, or fields with a specific annotation are formatted in a certain way. With a shared FormatterRegistry, you define these rules once and they are applied whenever formatting is needed. @@ -1125,16 +1127,16 @@ public interface FormatterRegistry { As shown above, Formatters can be registered by fieldType or annotation. FormattingConversionService is the implementation of FormatterRegistry suitable for most environments. - This implementation may be configured programatically or declaratively as a Spring bean with FormattingConversionServiceFactoryBean. - Because it also implements ConversionService, it can be configured for use with Spring's DataBinder as well as SpEL. + This implementation may be configured programatically, or declaratively as a Spring bean using FormattingConversionServiceFactoryBean. + Because this implemementation also implements ConversionService, it can be directly configured for use with Spring's DataBinder and the Spring Expression Language (SpEL).
Configuring Formatting in Spring MVC - In a Spring MVC application, you can configure a ConversionService instance explicity as an attribute of the annotation-driven element of the MVC namespace. - This ConversionService will then be used any time type conversion is needed during Controller model binding. - If not configured explicitly, Spring MVC will automatically register default formatters for common types such as numbers and dates. + In a Spring MVC application, you may configure a custom ConversionService instance explicity as an attribute of the annotation-driven element of the MVC namespace. + This ConversionService will then be used anytime a type conversion is required during Controller model binding. + If not configured explicitly, Spring MVC will automatically register default formatters and converters for common types such as numbers and dates. To rely on default formatting rules, no custom configuration is required in your Spring MVC config XML: @@ -1144,8 +1146,11 @@ public interface FormatterRegistry { + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/mvc + http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> @@ -1157,15 +1162,18 @@ public interface FormatterRegistry { Full support for the Joda Time formatting library is also installed if Joda Time is present on the classpath. - To inject a ConversionService instance with custom formatters/converters registered, set the conversion-service attribute: + To inject a ConversionService instance with custom formatters and converters registered, set the conversion-service attribute: + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/mvc + http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> @@ -1175,7 +1183,7 @@ public interface FormatterRegistry { ]]> - A custom ConversionService instance is often constructed by a FactoryBean, which internally registers custom Formatters and Converters programatically before the ConversionService is returned. + A custom ConversionService instance is often constructed by a FactoryBean that internally registers custom Formatters and Converters programatically before the ConversionService is returned. See FormatingConversionServiceFactoryBean for an example.