Java Reference
In-Depth Information
Tim Says. . .
Type Converters Can Return More Than One Type!
When you implement a TypeConverter, you implement it for a
specific type T. You may have noticed that the main method
you implement has a slightly different signature, though:
T convert(String in, Class<? extends T> targetType, ...);
Providing the target type at invocation time allows Stripes to
ask TypeConverter for instances of T , any subclasses of T , or, if
T is an interface, implementations of T . When performing type
conversion, Stripes will identify the declared type of a property
and supply it to the type converter.
This is how both PercentageTypeConverter and OneToManyType-
Converter know what type to return. PercentageTypeConverter
implements TypeConverter<Number> , but it can return, when
asked, float s, double s, and BigDecimal s. Since it returns a num-
ber between 0 and 1, it throws an exception if requested to
convert to any other numeric type. Similarly, the OneToMany-
TypeConverter class implements TypeConverter<Object> and can
return collections of any type for which the system has a regis-
tered TypeConverter .
Stripes will ask converters for subtypes only if you use a prop-
erty in your action bean for which Stripes doesn't have a Type-
Converter registered but can find a TypeConverter for a super-
class. Using this approach you can develop some pretty pow-
erful type converters. For example, if your domain objects all
extend from a base class or implement a common interface,
you can write a single TypeConverter to look them up from an ID
property.
 
 
Search WWH ::




Custom Search