4.7. Performing type conversion

When a model binding occurs during view postback, the binding system will attempt to convert the input value to the type of the target model property if necessary. Default Converters are registered for common types such as Numbers, primitives, enums, and Dates and are applied automatically. Users also have the ability to register their own converters for user-defined types, and to override the default Converters.

Implementing a Converter

To implement your own Converter, implement the org.springframework.binding.convert.converters.TwoWayConverter interface. A convenient StringToObject base class has been provided to simplify the implementation of this interface for converters that convert from a user input String to a user-defined Object and back. Simply extend from this class and override these two methods:

protected abstract Object toObject(String string, Class targetClass) throws Exception;

protected abstract String toString(Object object) throws Exception;			
			

toObject(String, Class) should convert from the input string to your object's type, and toString(Object) should do the reverse.

The following example shows a Converter that converts from String to a MonetaryAmount for working with currency values:

public class StringToMonetaryAmount extends StringToObject {

   public StringToMonetaryAmount() {
       super(MonetaryAmount.class);
   }

   @Override
   protected Object toObject(String string, Class targetClass) {
       return MonetaryAmount.valueOf(string);
   }

   @Override
   protected String toString(Object object) {
       MonetaryAmount amount = (MonetaryAmount) object;
       return amount.toString();
   }
}			
			

Review the pre-built converters in the org.springframework.binding.convert.converters package to see more examples of Converter implementations.

Registering a Converter

To install your own Converter or override any of the default Converters, extend from org.springframework.binding.convert.service.DefaultConversionService and override the addDefaultConverters() method. Use the addConverter(Converter) method to register the primary Converter to use to convert between two types, such as a String and a MonetaryAmount. Optionally use the addConverter(String, Converter) method to register alternate converters for the same type pair; for example, to support formatting a java.util.Date as a String in several different ways.

Each alternate Converter is indexed by a unique converterId that can be referenced when configuring a model binding. When no converter id is referenced explicitly by a binding, the primary Converter between the two types is always used.

The ConversionService is the object Web Flow consults at runtime to lookup conversion executors to convert from one type to another. There is generally one ConversionService per application. See the System Setup section for documentation on how to configure an extended ConversionService implementation that registers custom Converters to apply application-wide. Also consult the Convert API documentation for more information.