Fork me on GitHub
Edit on GitHub << back to Annotations

TypeConversion Annotation

This annotation is used for class and application wide conversion rules.

Usage

The TypeConversion annotation can be applied at method level, and since Struts 7.3.0 also at field level.

Key derivation

Since Struts 7.3.0

The key parameter accepts a bare property name for every ConversionRule; the rule’s prefix is derived automatically. It used to be derived only for method-level annotations, so class-level @Conversion(conversions = ...) entries had to spell the prefix out:

// before Struts 7.3.0 — prefix spelled out
@Conversion(conversions = @TypeConversion(key = "CreateIfNull_users", rule = ConversionRule.CREATE_IF_NULL, value = "true"))

// since Struts 7.3.0 — the CreateIfNull_ prefix is derived
@Conversion(conversions = @TypeConversion(key = "users", rule = ConversionRule.CREATE_IF_NULL, value = "true"))

Existing annotations that already carry a prefix keep working: a key starting with any known rule prefix is used as-is, never prefixed twice. The prefixes are CreateIfNull_, Element_, Key_ and KeyProperty_; ConversionRule.PROPERTY and ConversionRule.MAP have no prefix of their own. Keys of ConversionType.APPLICATION annotations are class names and are never prefixed.

Parameters

Parameter Required Default Description
key no The annotated property/key name The optional property name mostly used within TYPE level annotations.
type no ConversionType.CLASS Enum value of ConversionType. Determines whether the conversion should be applied at application or class level.
rule no ConversionRule.PROPERTY Enum value of ConversionRule. The ConversionRule can be a property, a Collection or a Map.
converter DEPRECATED: either this or value   The class name of the TypeConverter to be used as converter.
converterClass either this or value   The class of the TypeConverter to be used as converter. XWorkBasicConverter by default.
value either converter or this   The value to set for ConversionRule.KEY_PROPERTY.

ConversionRule.COLLECTION is deprecated since Struts 7.3.0 — use ConversionRule.ELEMENT instead. The two are handled identically by the engine, and ELEMENT additionally covers the values of a Map. Existing annotations using COLLECTION keep working and produce the deprecated Collection_xxx key, which is still read as a fallback.

Examples

 @Conversion()
 public class ConversionAction implements Action {

   private String convertInt;

   private String convertDouble;
   private List users = null;

   private HashMap keyValues = null;

   @TypeConversion(type = ConversionType.APPLICATION)
   @StrutsParameter
   public void setConvertInt( String convertInt ) {
       this.convertInt = convertInt;
   }

   @TypeConversion(converterClass = XWorkBasicConverter.class)
   @StrutsParameter
   public void setConvertDouble( String convertDouble ) {
       this.convertDouble = convertDouble;
   }

   @TypeConversion(rule = ConversionRule.ELEMENT, converterClass = String.class)
   @StrutsParameter
   public void setUsers( List users ) {
       this.users = users;
   }

   @TypeConversion(rule = ConversionRule.MAP, converterClass = BigInteger.class)
   @StrutsParameter
   public void setKeyValues( HashMap keyValues ) {
       this.keyValues = keyValues;
   }

   @TypeConversion(type = ConversionType.APPLICATION, property = "java.util.Date", converterClass = XWorkBasicConverter.class)
   public String execute() throws Exception {
       return SUCCESS;
   }
 }
Follow @x