Struts2 action supports automatic conversion of instance variables of Enumeration type (enum)

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.