注册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.properties
java.util.Date=cn.aweb.awebvideo.meetingRealTime.util.DateConverter

参考资料
lunzi   2008-11-26 17:06:06 评论:0   阅读:693   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

认证码: (如不清楚,请刷新)


Copyright@2004-2010 powered by YuLog