当前位置: 代码迷 >> Web前端 >> 登记webwork/struts2的类型转换器
  详细解决方案

登记webwork/struts2的类型转换器

热度:144   发布时间:2012-09-13 09:51:53.0
注册webwork/struts2的类型转换器
以全局时间统一控制为例:
时间转换器

package cn.aweb.awebvideo.meetingRealTime.util;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import ognl.DefaultTypeConverter;import com.opensymphony.xwork.XworkException;public class DateConverter extends DefaultTypeConverter { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");        public Object convertValue(Map ognlContext, Object value, Class toType) {           Object result = null;           if (toType == Date.class) {               result = doConvertToDate(value);           } else if (toType == String.class) {               result = doConvertToString(value);           }           return result;       }         private Date doConvertToDate(Object value) {           Date result = null;             if (value instanceof String) {                   try { result = sdf.parse((String) value); } catch (java.text.ParseException e) { throw new XworkException("Could not parse date", e); }                             } else if (value instanceof Object[]) {               Object[] array = (Object[]) value;               if ((array != null) && (array.length >= 1)) {                   value = array[0];                   result = doConvertToDate(value);               }           } else if (Date.class.isAssignableFrom(value.getClass())) {               result = (Date) value;           }           return result;       }         private String doConvertToString(Object value) {           String result = null;           if (value instanceof Date) {               result = sdf.format(value);           }           return result;       }   }注册需要的配置文件xwork-conversion.propertiesjava.util.Date=cn.aweb.awebvideo.meetingRealTime.util.DateConverter
  相关解决方案