Today, I saw a lot of code written by programmers, which is always converted back and forth between enum and string. I am not sure whether struts2 can convert the enum type. The struts2 documentation does not support automatic conversion of enum, by reading the source code of DefaultTypeConverter of struts2, we can find that it is okay, mainly in the convertValue method.
View plaincopy to clipboardprint? Public Object convertValue (Object value, Class toType ){
Object result = null;
If (value! = Null ){
/* If array-> array then convert components of array individually */
If (value. getClass (). isArray () & toType. isArray ()){
Class componentType = toType. getComponentType ();
Result = Array. newInstance (componentType, Array
. GetLength (value ));
For (int I = 0, icount = Array. getLength (value); I <icount; I ++ ){
Array. set (result, I, convertValue (Array. get (value, I ),
ComponentType ));
}
} Else {
If (toType = Integer. class) | (toType = Integer. TYPE ))
Result = Integer. valueOf (int) longValue (value ));
If (toType = Double. class) | (toType = Double. TYPE ))
Result = new Double (doubleValue (value ));
If (toType = Boolean. class) | (toType = Boolean. TYPE ))
Result = booleanValue (value )? Boolean. TRUE: Boolean. FALSE;
If (toType = Byte. class) | (toType = Byte. TYPE ))
Result = Byte. valueOf (byte) longValue (value ));
If (toType = Character. class) | (toType = Character. TYPE ))
Result = new Character (char) longValue (value ));
If (toType = Short. class) | (toType = Short. TYPE ))
Result = Short. valueOf (short) longValue (value ));
If (toType = Long. class) | (toType = Long. TYPE ))
Result = Long. valueOf (longValue (value ));
If (toType = Float. class) | (toType = Float. TYPE ))
Result = new Float (doubleValue (value ));
If (toType = BigInteger. class)
Result = bigIntValue (value );
If (toType = BigDecimal. class)
Result = bigDecValue (value );
If (toType = String. class)
Result = stringValue (value );
If (Enum. class. isAssignableFrom (toType ))
Result = enumValue (Class <Enum>) toType, value );
}
} Else {
If (toType. isPrimitive ()){
Result = primitiveDefaults. get (toType );
}
}
Return result;
}
Author: "One World" Scholar"