Struts2.0的不同版本针对枚举转换器应用
???? Struts2.0 的Apache项目中从Struts2.1.0开始不再支持枚举类型的自动转换,所以项目中枚举转换采用自己开发扩展的枚举转换器:不再支持的主要原因为:
?? @deprecated Since Struts 2.1.0 as enum support is now built into XWork
?
?从Struts 2.1.0 开始采用不再支持枚举类型XWork 框架
?
本人重点讲述:
Struts2.1.0的枚举转换器的:
如下:
?
?
@Deprecated public class EnumTypeConverter extends DefaultTypeConverter {
??? /**
???? * Converts the given object to a given type. How this is to be done is implemented in toClass. The OGNL context, o
???? * and toClass are given. This method should be able to handle conversion in general without any context or object
???? * specified.
???? *
???? * @param context - OGNL context under which the conversion is being done
???? * @param o?????? - the object to be converted
???? * @param toClass - the class that contains the code to convert to enumeration
???? * @return Converted value of type declared in toClass or TypeConverter.NoConversionPossible to indicate that the
???? *???????? conversion was not possible.
???? */
??? @Override
??? public Object convertValue(Map<String, Object> context, Object o, Class toClass) {
??????? if (o instanceof String[]) {
??????????? return convertFromString(((String[]) o)[0], toClass);
??????? } else if (o instanceof String) {
??????????? return convertFromString((String) o, toClass);
??????? }
??????? return super.convertValue(context, o, toClass);
??? }
??? /**
???? * Converts one or more String values to the specified class.
???? * @param value - the String values to be converted, such as those submitted from an HTML form
???? * @param toClass - the class to convert to
???? * @return the converted object
???? */
??? public java.lang.Enum convertFromString(String value, Class toClass) {
??????? return Enum.valueOf(toClass, value);
??? }
}
?
?
Struts2.1.0以后创建自定义的枚举扩展类:不过采用StrutsTypeConverter
public class GenericEnumConverter extends StrutsTypeConverter :
?
public Object convertFromString(Map context, String[] values, Class toClass) :
public String convertToString(Map context, Object o) :方法
?
同时在需要的枚举类型时在全局转换器xwork-conversion.properties中配置:
自定义枚举类型类全路径=自定义枚举转换器
?
?
?