Introduction to Type conversion in Struts
Posted on: January 27, 2011 at 12:00 AM
In this tutorial you will learn about the type conversion in struts2.2.1

Introduction to Type conversion in Struts

Type conversion is a mechanism or a process in which data is converted into one object type to other object type. Struts2.2.1 provide this feature to translate the request parameters to some other object which  they represents. Many web technologies provides this powerful and important facility. In struts the type conversion is an mechanism which helps in translating the request parameters. These request parameters are converted into complex bean properties using this type conversion mechanism.

Struts by default provides the type conversion of primitives data, a simple object and collections. To use type conversion mechanism you need to do the following in your web application.

At first make an action class which handles the request parameters.

Then make a properties files of same name as your ClassName - For example

The properties file must remain inside the same package where the your action class.

Then make a Conversion class which converts your request parameters into a complex bean object.

To make a Converter class you can extend DefaultTypeConverter or implement TypeConverter. Both class and interface provides convertValue method which converts your request parameters to object and returns  this object to bean.

To convert your request parameter into an object you need to pass parameter value to the Converter class class. To pass the value you write into ClassName - file as.

variableName=net.roseindia.converter.TestConverter. (Here you need to specify your Converter class with fully qualified package name)

Then the converter class takes the request parameters value and converts into a object.

An example of type conversion is given in the next section of the tutorial.

Related Tags for Introduction to Type conversion in Struts: