现在的位置: 首页 > 综合 > 正文

struts2的时间格式转换问题

2013年10月19日 ⁄ 综合 ⁄ 共 5727字 ⁄ 字号 评论关闭

关键字: struts2

    struts2提供了一个时间标签:

Xml代码 复制代码
  1. <s:date name="todayDate" format="yyyy-MM-dd" />  

   但这个标签很只能在显示的时候用,但如果我想在输入框里显示时间,让用户直接修改时间,怎么弄?datepicker?选择太麻烦,我想让用户输入,并且兼容多种日期格式。还有,如果用时间标签的话,每个地方都需要指定format,如果我想修改一下格式,所有的时间显示都变,怎么弄?

 

翻了一下struts2的源码,和文档,找到一个办法。  com.opensymphony.xwork2.util.XWorkConverter

Java代码 复制代码
  1. * <p/> In some situations you may wish to apply a type converter globally.    
  2. *  This can be done by editing the file   
  3. * <b>xwork-conversion.properties</b> in the root of your class path    
  4. * (typically WEB-INF/classes) and providing a   
  5. * property in the form of the class name of the object you wish to convert    
  6. * on the left hand side and the class name of   
  7. * the type converter on the right hand side. For example, providing    
  8. * a type converter for all Point objects would mean   
  9. * adding the following entry:   
  10. *   
  11. * <p/><b>com.acme.Point = com.acme.PointConverter</b>  

 

XWorkConverter,先在classpath root下找xwork-conversion.properties文件,这个文件定义了全局转换。然后每遇到新的类需要转换,便查找是否有特殊的自定义转换配置。特殊自定义转换配置文件的路径是:

Java代码 复制代码
  1. className.replace('.''/') + "-conversion.properties";  

 比方com.acme.Point的转换配置就是classpath 下的/com/acme/Point-coversion.properties文件。

 

ok,这个问题好解决了。

 

我的xwork-coversion.properties文件:

Xml代码 复制代码
  1. java.util.Date=cn.jolstar.struts.type.DateTypeConverter  

 我的DateTypeConverter代码:

Java代码 复制代码
  1. /**  
  2.  *   
  3.  */  
  4. package cn.jolestar.struts.type;   
  5.   
  6. import java.text.DateFormat;   
  7. import java.text.ParseException;   
  8. import java.text.SimpleDateFormat;   
  9. import java.util.Date;   
  10. import java.util.Map;   
  11.   
  12. import org.apache.log4j.Logger;   
  13. import org.apache.struts2.util.StrutsTypeConverter;   
  14.   
  15.   
  16. /**  
  17.  * @author jolestar  
  18.  *   
  19.  */  
  20. public class DateTypeConverter extends StrutsTypeConverter {   
  21.   
  22.     private static final Logger log = Logger.getLogger(DateTypeConverter.class);   
  23.     public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";   
  24.        
  25.     //暂时只考虑这几种日期格式   
  26.     public static final DateFormat[] ACCEPT_DATE_FORMATS = {   
  27.             new SimpleDateFormat(DEFAULT_DATE_FROMAT),   
  28.             new SimpleDateFormat("yyyy年MM月dd日"),   
  29.             new SimpleDateFormat("yyyy/MM/dd") };   
  30.   
  31.     /**  
  32.      *   
  33.      */  
  34.     public DateTypeConverter() {   
  35.     }   
  36.   
  37.     /*  
  38.      * (non-Javadoc)  
  39.      *   
  40.      * @see org.apache.struts2.util.StrutsTypeConverter#convertFromString(java.util.Map,  
  41.      *      java.lang.String[], java.lang.Class)  
  42.      */  
  43.     @Override  
  44.     public Object convertFromString(Map context, String[] values, Class toClass) {   
  45.         if (values[0] == null || values[0].trim().equals(""))   
  46.             return null;   
  47.         for (DateFormat format : ACCEPT_DATE_FORMATS) {   
  48.             try {   
  49.                 return format.parse(values[0]);   
  50.             } catch (ParseException e) {   
  51.                 continue;   
  52.             } catch (RuntimeException e) {   
  53.                 continue;   
  54.             }   
  55.         }   
  56.         log.debug("can not format date string:" + values[0]);   
  57.         return null;   
  58.     }   
  59.   
  60.     /*  
  61.      * (non-Javadoc)  
  62.      *   
  63.      * @see org.apache.struts2.util.StrutsTypeConverter#convertToString(java.util.Map,  
  64.      *      java.lang.Object)  
  65.      */  
  66.     @Override  
  67.     public String convertToString(Map context, Object o) {   
  68.         if (o instanceof Date) {   
  69.             SimpleDateFormat format = new SimpleDateFormat(   
  70.                     DEFAULT_DATE_FORMAT);   
  71.             try {   
  72.                 return format.format((Date) o);   
  73.             } catch (RuntimeException e) {   
  74.                 return "";   
  75.             }   
  76.         }   
  77.         return "";   
  78.     }   
  79.   
  80. }  

 

 

 

这样,从字符串转换为日期对象的时候,会尝试上面列出的多种日期格式,但输出的时候,则会统一按照DEFAULT—DATE—FORMAT转换。 需要修改格式,只需要修改DEFAULT—DATE—FORMAT。当然,你也可以把它方在配置文件里,便于修改。

 

了解了这一点,其实也就 明白了struts的类型转换模式。然后,无论是字符串id到持久化对象的转换,还是自定义的字符串到对象之间的转换,都容易了。

【上篇】
【下篇】

抱歉!评论已关闭.