STRUTS2 custom type converters are divided into local type converters and global type converters
(1) Local type converter
If the page comes with a parameter reg.action?birthday=2010-11-12 to the background action, then the property can be received with the date type, but if a string of type 20101112 is passed, the date type is not available, And there will be an error, STRUTS2 provides a type converter for us to use.
The following is a development step for a local type converter
A. First write a class to inherit Defaulttypeconverter
B. Then overwrite the Convertvalue method and transform the data inside
C. Place the Actionclassname-conversion.properties file under the package where the action class is located, Actionclassname is the class name, and the following-conversion.properties is a fixed notation,
such as: helloworldaction-conversion.properties
The contents of the D.properties file are: property name = The full class name of the type converter (both the package name. Class name)
such as: Birthday=com.ljq.type.converter.datetypeconverter
(2) Global type converter
If the business needs all dates to be converted, you can use a global type converter, as long as the xwork-conversion.properties file is placed under the SRC root directory, and the contents of the properties file are:
Type to convert = Full class name of the type converter
such as: Java.util.Date = Com.type.Converter.DateTypeConverter can be
Code
Action class
Package Com.ljq.action;import Java.util.date;public class Helloworldaction {//dates private date birthday;//enumeration private Gender gender;public String Execute () {return "Success";} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {System.out.println ("birthday=" +birthday); this.birthday = Birthday;} Custom enumeration Public enum Gender {man,women}public Gender getgender () {return Gender;} public void Setgender (Gender Gender) {System.out.println ("gender=" +gender); this.gender = Gender;}}
Date type Converters
Package Com.ljq.type.converter;import Java.text.simpledateformat;import Java.util.date;import java.util.Map;import com.opensymphony.xwork2.conversion.impl.defaulttypeconverter;/*** Date Custom Type converter * * @author Jiqinlin**/public class Datetypeconverter extends Defaulttypeconverter {@SuppressWarnings ("unchecked") @Overridepublic Object Convertvalue ( Map<string, object> context, Object Value,class totype) {SimpleDateFormat SDF = new SimpleDateFormat ("YyyyMMdd hh:m M:ss "), try {if (ToType = = Date.class) {//when the string is converted to a Date type string[] params = (string[]) Value;return Sdf.parseobject (params[ 0]);} else if (ToType = = String.class) {//when date is converted to a string, date date= (date) Value;return Sdf.format (date);}} catch (Java.text.ParseException e) {e.printstacktrace ();} return null;}}
Enum type converter
Package Com.ljq.type.converter;import Java.util.map;import Com.ljq.action.helloworldaction.gender;import com.opensymphony.xwork2.conversion.impl.defaulttypeconverter;/*** Enumerating Custom Type converters * * @author Jiqinlin**/public class Gendertypeconverter extends defaulttypeconverter{@Overridepublic Object convertvalue (map<string, object> Context, Object Value,class ToType) {if (Totype==gender.class) {///when the string is converted to the Gender type string[] params= (string[]) value; Return gender.valueof (Params[0]);} else if (totype==string.class) {///when gender is converted to a string gender gender= (gender) Value;return gender.tostring ();} return null;}}
Configuring type Converters
Test Path Date http://localhost:8083/struts2/control/employee/list_execute.do?birthday=20110315 23:34:55 enumeration/HTTP// Localhost:8083/struts2/control/employee/list_execute.do?gender=women Local type converters: Helloworldaction-conversion.propertiesbirthday=com.ljq.type.converter.datetypeconvertergender= Com.ljq.type.converter.GenderTypeConverter Global type converter: xwork-conversion.propertiesjava.util.date= Com.ljq.type.converter.DateTypeConverter
The page prints the date and the value of the enumeration
Birthday=${birthday}
Gender=${gender}
STRUTS2 Custom Type Converters