diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index f81bf35ec25..04357496dc7 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -754,7 +754,7 @@ public final class CustomPropertyEditorRegistrar implements PropertyEditorRegist Spring 3 introduces a core.convert package that provides a general type conversion system. The system defines a SPI to implement type conversion logic, as well as a API to execute type conversions at runtime. Within a Spring container, if configured, this system can be used as an alternative to PropertyEditors to convert externalized bean property value strings to required property types. - The public API may also be used anywhere in your application where type coersion is needed. + The public API may also be used anywhere in your application where type conversion is needed.
Converter SPI @@ -769,12 +769,12 @@ public interface Converter { }]]> - To create your own Converter, simply implement the Converter interface. + To create your own Converter, simply implement the interface above. Parameterize S as the type you are converting from, and T as the type you are converting to. For each call to convert(S), the source argument is guaranteed to be NOT null. Your Converter may throw any Exception if conversion fails. An IllegalArgumentException is often thrown to report an invalid source value. - Take special care to ensure your Converter implementation is thread safe. + Take care to ensure your Converter implementation is thread safe. Several converter implementations are provided in the core.convert.converters package as a convenience. @@ -791,6 +791,47 @@ public class StringToInteger implements Converter { }]]>
+
+ ConverterFactory + + When you need to centralize the conversion logic for an entire class hierarchy, for example, when converting from String to java.lang.Enum objects, implement a ConverterFactory: + + { + Converter getConverter(Class targetType); +}]]> + + + Parameterize S to be type you are converting from, and R to be base type defining the range of classes you can convert to. + Then implement getConverter(Class<T>T), where T is a subclass of R. + + + Note the StringToEnum ConverterFactory as an example: + + { + + public Converter getConverter(Class targetType) { + return new StringToEnum(targetType); + } + + private final class StringToEnum implements Converter { + + private Class enumType; + + public StringToEnum(Class enumType) { + this.enumType = enumType; + } + + public T convert(String source) throws Exception { + return (T) Enum.valueOf(this.enumType, source.trim()); + } + } +}]]> + +
ConversionService API @@ -809,7 +850,7 @@ public interface ConversionService { Most ConversionService implementations also implement ConverterRegistry, which provides a SPI for registering converters. - Internally, a ConversionService implementation delegates to its registered Converters to carry out type conversion logic. + Internally, a ConversionService implementation delegates to its registered Converters and ConverterFactories to carry out type conversion logic. Two ConversionService implementations are provided with the system in the core.convert.support package. @@ -870,7 +911,7 @@ public class MyService {
Spring 3 UI Field Formatting - The core.convert is a simple, general-purpose type conversion system. + core.convert is a simple, general-purpose type conversion system. It addresses one-way conversion 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 when binding bean property values. In addition, the Spring Expression Language (SpEL) uses this system to coerce Expression values. @@ -953,7 +994,7 @@ public final class DateFormatter implements Formatter { The Spring team welcomes community-driven Formatter contributions; see http://jira.springframework.org to contribute. - In particular, the team expects to integrate support for Joda Time and Money Formatters in the future. + In particular, the team hopes to integrate support for Joda Time and Money Formatters in the future.
@@ -1036,7 +1077,7 @@ public class MyModel { type, Formatter targetFormatter); + void add(Formatter formatter); void add(AnnotationFormatterFactory factory); }]]> @@ -1099,11 +1140,14 @@ public interface FormatterRegistry { To apply a Formatter to a single field, create an @InitBinder callback on your @Controller, then call binder.registerFormatter(String, Formatter):