Struts2.0 built-in Converter
Struts 2.0 has implemented a frequently used converter, such as a date, integer, or floating point number. The following lists the implemented converters.
L predefined types, such as int, Boolean, and double;
L date type: use the short format conversion of the current region (locale), that is, dateformat. getinstance (dateformat. Short );
L collection type, which converts the string data returned by request. getparametervalues (string Arg) to Java. util. collection;
L set type, similar to list conversion, with the same value removed;
L array type: converts each element of the string array to a specific type and forms an array.
For existing converters, you do not have to re-invent the wheel. Struts will automatically call the corresponding converter when encountering these types.
Custom Converter
To convert a custom type, you need to implement the strutstypeconverter class. There are two methods to be implemented. One is to convert strings into new classes, and the other is to convert new types into strings. For example:
Public class mytypeconverter extends strutstypeconverter { Public object convertfromstring (MAP context, string [] values, class toclass ){ ... } Public String converttostring (MAP context, object O ){ ... } } |
Before using the new converter in action, you need to configure it. A separate "*-conversion. properties" file can be used here. If the action that is using the conversion function is called "myaction", you need to create a file named "MyAction-conversion.properties" under the same package. File Content:
Typeval = mytypeconverter
The "typeval" on the left of the equation is the request name or the form value to be converted, and the right of the equation is the fully qualified name of the conversion class.
If you use the Transform Feature across multiple actions, you can use a global profile "xwork-conversion.properties ". This file is stored in the classpath root directory of the application. File Content:
Mytype = mytypeconverter
The equation is the fully qualified name of the class corresponding to the type to be converted (the full name of the type to be converted (including the package path and class name) and the converter. Note that the class name of the type to be converted is used here, instead of the Request Name or form value. Therefore, the setter of the action is "settypevalue (mytype type )".