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中配置:

自定义枚举类型类全路径=自定义枚举转换器

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值