Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Clarified as per WW-2613 Thanks Steve.

...

Table of Contents
minLevel2
outlinetrue
stylenone

A Simple Example

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork2.conversion.impl.XWorkConverter}
Note
Wiki Markup
{snippet:id=i18n-note|javadoc=true|url=com.opensymphony.xwork2.conversion.impl.XWorkConverter}

The framework ships with a base helper class that simplifies converting to and from Strings, org.apache.struts2.util.StrutsTypeConverter. The helper class makes it easy to write type converters that handle converting objects to Strings as well as from Strings.

From the JavaDocs:

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=org.apache.struts2.util.StrutsTypeConverter}

Built in Type Conversion Support

...

The best way to take advantage of the framework's type conversion is to utilize complete objects (ideally your domain objects directly). There is no need to capture form values using intermediate Strings and primitives and then convert those values to full objects in an Action method.

...

Creating a Type Converter

To create Create a type converter one would need to extends StrutsTypeConverterby extending StrutsTypeConverter. The Converter's role is to convert a String to an Object and an Object to a String.

Code Block
 public class MyConverter extends StrutsTypeConverter {
    public Object convertFromString(Map context, String[] values, Class toClass) {
       .....
    }

    public String convertToString(Map context, Object o) {
       .....
    }
 }
Note

To allow Struts to recognize that a conversion error has occurred, the converter class need needs to throw XWorkException or preferably TypeConversionException.

Applying a Type Converter to an Action

Create a file called 'ActionClassName-conversion.properties' in the same location of the classpath as the Action class itself resides.

Eg. if the action class name is MyAction, the action-level conversion properties file should be named 'MyAction-conversion.properties'. If the action's package is com.myapp.actions the conversion file should also be in the classpath at /com/myapp/actions/.

Within the conversion file, name the action's property and the Converter to apply to it:

Code Block

point = com.acme.PointConverter 

Type conversion can also be specified via Annotations within the action.

A Simple Example

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork2.conversion.impl.XWorkConverter}
Note
Wiki Markup
{snippet:id=i18n-note|javadoc=true|url=com.opensymphony.xwork2.conversion.impl.XWorkConverter}

The framework ships with a base helper class that simplifies converting to and from Strings, org.apache.struts2.util.StrutsTypeConverter. The helper class makes it easy to write type converters that handle converting objects to Strings as well as from Strings.

From the JavaDocs:

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=org.apache.struts2.util.StrutsTypeConverter}

Advanced Type Conversion

The framework also handles advanced type conversion cases, like null property handling and converting values in Maps and Collections, and type conversion error handling.

...