Custom type converters must implement Ongl. TypeConverter interface or a specific implementation of this interface to extendextended Strutstypeconverter classIn most type converters, you need to provide conversion functionality from string types to non-string types and vice versa
There are two abstract methods in Strutstypeconverter: simple example: I: Creating a mytypeconverter converter is the function of converting a string into a number.
Public
classMytypeconverter
extendsStrutstypeconverter { @Override
PublicObject convertfromstring ( Map context, string[] values, Class toclass) {System.
out. println ("Values:"+ values[0]);
intparseint = Integer.
parseint (Values[0]);
returnparseint; } @Override
PublicString converttostring ( Map context, Object o) {System.
out. println (o);
return
NULL ; } }II: There is an attribute in the action
Public
classuseraction
extends Actionsupport
Implementspreparable,Modeldriven<user>, Requestaware {
PrivateInteger Num;
Public
voidsetnum (Integer num) {System.
out. println ("User NUm:"+num);
This.Num= num; }
PublicInteger Getnum () {
returnNum; } }III: Create a Property file:useraction-conversion.propertiesTo edit the properties file:num=Mytypeconverter. MyType The content in the properties file is:
Property name = Full class name of the type converter
For this example, the name of the file should beuseraction-conversion.properties2: Based on class configuration (Global): Create the Xwork-conversion.properties file in the web-inf/classes/directory. "
The content in the properties file is:
type to convert = Full class name of the type converter
For the purposes of this example,xwork-conversion.propertiesThe contents of the file are:java.lang.integer=Typecovert.Mytypeconverter
Custom type Converters