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. ADS_TO_REPLACE_1
Then make a properties files of same name as your ClassName - conversion.properties. For example TestClass-conversion.properties.
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. ADS_TO_REPLACE_2
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 - conversion.properties file as.
variableName=net.roseindia.converter.TestConverter. (Here you need to specify your Converter class with fully qualified package name)ADS_TO_REPLACE_3
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.
Advertisements
Ads
Ads