2017年4月20日 星期四

Struts Converter

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

1. 先建立轉換器的Class
  1. public class DateConvert extends DefaultTypeConverter{
  2. @Override
  3. @SuppressWarnings("rawtypes")
  4. public Object convertValue(Map context, Object value, Class toType) {
  5. if(Date.class.equals(toType)){
  6. DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.TAIWAN);
  7. String[] str = (String[]) value;
  8. try {
  9. if(str != null && ! StringUtils.isBlank(str[0])){
  10. return df.parse(str[0]);
  11. }else{
  12. return null;
  13. }
  14. } catch (ParseException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. /*
  19. // 可以在同一個class針對不同的type做處理
  20. // 不過個人比較偏好一種寫一個class (有需求的話)
  21. else if(String.class.equals(toType)){
  22. do something
  23. }
  24. */
  25. return null;
  26. }
  27. }

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,

    回覆刪除