How to make Custom converter in Struts2 ?

To make custom converter for any specific user definied type it need to make a java class which extends the framework's provided class 

"org.apache.struts2.util.StrutsTypeConverter"

this class has following two abstract methods which need to implement with the required code difinition as per the requirement of conversion.

public abstract Object convertFromString(Map context, String[] values, Class toClass);
public abstract String convertToString(Map context, Object o);

For any conversion to specific type it need to convert from string to specific type when submitting data using Form to the action class.
and from specific user definied type to the String when displaying on the view (JSP).

Example

public class CircleTypeConverter extends StrutsTypeConverter {

public Object convertFromString(Map context, String[] values, Class toClass) {
String userString = values[0];
Circle newCircle = parseCircle ( userString );
return newCircle;
}

public String convertToString(Map context, Object o) {
Circle circle = (Circle) o;
String userString = "C:r" + circle.getRadius();
return userString;
}

private Circle parseCircle( String userString ) throws TypeConversionException
{
Circle circle = null;
int radiusIndex = userString.indexOf('r') + 1;
if (!userString.startsWith( "C:r") )
throw new TypeConversionException ( "Invalid Syntax");

int radius;
try {
radius = Integer.parseInt( userString.substring( radiusIndex ) );
}catch ( NumberFormatException e ) {
throw new TypeConversionException ( "Invalid Value for Radius"); 
}
circle = new Circle();
circle.setRadius( radius );
return circle;
}
}


After creating the required converter class, user need to register that class into the 
ActionClassName-conversion.properties file with the respective user defined type.

Here you need to add following line into the ActionClassName-conversion.properties file
com.data.circle=com.custom.converter.CircleTypeConverter

This converter will be local only for the specifig action class only. 
To make the global converter for whole application.

You need to add following line into xwork-conversion.properties file and this file must be located at the WEB-INF/classes folder.
com.data.circle=com.custom.converter.CircleTypeConverter

After following all of above steps OGNL will come to know which type converter it should use for the Circle type.