One, custom type converter
1, write a class, inherit Com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter
2, covering out the public Object convertvalue (map<string, object> context, Object Value,class ToType)
CONTEXT:OGNL The context of an expression
Value: The actual value. The user input is a string, but he is an array of string.
totype: Target type
PUBLIC&NBSP;CLASS&NBSP;DATECONVERTOR&NBSP;EXTENDS&NBSP;DEFAULTTYPECONVERTER&NBSP;{/*&NBSP;CONTEXT:OGNL the context of an expression value: The values entered by the user ( when data is saved) or properties in the model. The value entered by the user is a String array totype: The target type */@Overridepublic object convertvalue (map<string, Object> context, object value,class totype) { dateformat df = new simpledateformat ("Yyyy/mm/dd"); if (ToType==Date.class) { String strValue = ((string[]) value) [0]; try { return df.parse (StrValue); } catch (parseexception e) { throw new runtimeexception (e); } }else{ date dvalue = (Date) value; return Df.format (dvalue); } }}
3. Register type Converter
3.1 Local type converter: Valid only for the current action
Practice: In the same package as the action class, a configuration file named "Action class name-conversion.properties" is created,
File, add the following: field to verify = Full class name of validator
Birthday=cn.itcast.convertor.dateconvertor
3.2 Global type converter: valid for all action
Practice: Under the Web-inf/classes directory, create a configuration file called "Xwork-conversion.properties",
The following is added to the file: Full name of the target type = Full class name of the validator
Java.util.date=cn.itcast.convertor.dateconvertor
Note: If the conversion fails, the STRUTS2 framework looks for the Name=input results page
Custom STRUTS2 type Converters