2017年4月20日 星期四

Struts Converter

原始問題:
Client端的時區是美國
Server端時區是台灣
當Client Submit資料時,Date就會有時區的差異沒辦法直接使用
所以配置了針對Date格式的轉換器去固定時區

1. 先建立轉換器的Class
public class DateConvert extends DefaultTypeConverter{
    @Override
    @SuppressWarnings("rawtypes")
    public Object convertValue(Map context, Object value, Class toType) {
        if(Date.class.equals(toType)){
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.TAIWAN);

            String[] str = (String[]) value;

            try {
                if(str != null && ! StringUtils.isBlank(str[0])){
                    return df.parse(str[0]);
                }else{
                    return null;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        /*
        // 可以在同一個class針對不同的type做處理
        // 不過個人比較偏好一種寫一個class (有需求的話)
        else if(String.class.equals(toType)){
            do something
        }
        */
        return null;
    }
}

2. 建立struts2的配置文件xwork-conversion.properties,放在src路徑下
java.util.Date=pageName.DateConvert



參考連結:
http://www.blogjava.net/max/archive/2013/04/10/79602.html
http://www.cnblogs.com/lpshou/archive/2012/11/27/2791188.html

1 則留言:

  1. Lucky Club: The Casino Site that Lets You Play, win
    Lucky Club is a brand new online casino which is bringing the fun of a world-class 카지노사이트luckclub casino to the palm of your hand. You can play online,

    回覆刪除