• how conversion works (plugin in through Ognl)
  • how to have custom converters
  • examples

XWork2 supports validation at a global and action level

How it works

XWork2 make use of Ognl to do its conversion, by plugging in XWork2 specifi PropertyAccessor, MethodAccessor, NullHandler and TypeConverter etc. into Ognl runtime.

Global Level

To create a global level conversion, create an file called 'xwork-conversion.properties' at the root level of classpath. The entry in 'xwork-conversion.properties' should be a key-value pair just like any Java property file, where the key represents the FQN (Fully-Qualified-Name) of a particular object where conversion is to be done, whereas the value would be the FQN of the XWork2 converter itself.

The following is an example of a hypotenical 'xwork-conversion.properties'

....
 java.lang.Boolean=foo.bar.MyBooleanConverter
 foo.bar.MyObject=foo.bar.MyObjectConverter
 ...

 

Action Level

To create an action-level conversion, create a file called 'ActionClassName-conversion.properties' in the same location at the classpath where the Action class itself resides. Eg. if the action class name is MyAction, the action-level convertion properties file should be named 'MyAction-conversion.properties'.

Assuming that 'MyAction' action class looks as follows. Note the properties it has.

public class MyAction extends ActionSupport {
   private Boolean myBool;
   private Double myDouble;

   public Boolean getMyBool() { return this.myBool; }
   public void setMyBool(Boolean myBool) { this.myBool = myBool; }

   public Double getMyDouble() { return this.myDouble; }
   public void setMyDouble(Double myDouble) { this.myDouble = myDouble; }

   .....
 }

 

The following would an example of a hypotenical 'MyAction-conversion.properties'.

myBool=foo.bar.MyBooleanConverter
myDouble=foo.bar.MyDoubleConverter

 

Custom Converter

The following is an example of a converter. It extends DefaultTypeConverter (part of Ognl).

public class MyConverter extends DefaultTypeConverter {
   public Object convertValue(Map context, Object target, Member member, String propertyName,
                              Object value, Class toType) {
       // type conversion goes here.
       ....
   }
 }