struts2的action支持自动转换枚举类型(enum)的实例变量
2011-09-16 17:03
375 查看
今天看到很多程序员写的代码,老是在enum与string之间来回转换,自己也不确定struts2能否进行enum的类型转换,struts2的文档说不支持enum的自动转换,通过阅读struts2的DefaultTypeConverter源代码发现是可以的,主要集中在convertValue方法
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;
}
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;
}
相关文章推荐
- struts2新增json返回类型,自动将action中的的成员变量转换成json字符串
- struts2页面到action中date类型自动转换
- struts2页面到action中date类型自动转换
- struts2页面到action中date类型自动转换
- Struts2数据在jsp和action间自动转换
- Java多态 实例子类自动调用父类为空的构造方法 成员变量不支持Override 可写,没多态效果
- 由Spring管理的Struts2的Action的单实例问题
- struts2使用action方式实现文件下载(支持中文文件名)
- Struts2数据转换实例之数组转换
- VS2008不能自动绑定添加非控件变量,VS2010编译的程序不支持Windows2000
- 简单的Struts2的Interceptors(拦截器)机制介绍以及利用拦截器实现用户登陆时执行耗时操作并自动跳转的实例
- poi excel自动转换成javabean 支持引用类型属性二级转换
- Struts2单选按钮详解及枚举类型的转换代码示例
- struts2与spring集成时,关于Action类中成员bean自动注入的问题
- AVA学习笔记之变量类型转换和类型自动提升
- java的枚举类型(enum)实例
- STRUTS2为每个线程提供一个ACTION实例
- dedecms联动模型支持dede_sys_enum的father字段自动追加内容
- Myeclipse中struts2配置文件配置DTD以支持自动补全
- 关于@property自动生成的实例变量的相关问题