struts2的时间格式转换问题

news/2024/7/1 13:55:13 标签: struts, date, string, class, object, null
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

class="" title="">

关键字: class="tags" href="/tags/STRUTS.html" title=struts>struts2
class="blog_content">

    class="tags" href="/tags/STRUTS.html" title=struts>struts2提供了一个时间标签:

class="dp-highlighter">
class="bar">
class="tools"> Xml代码 复制代码
    class="dp-xml">
  1. class="tag"><class="tag-name">s:class="tags" href="/tags/DATE.html" title=date>date class="attribute">name=class="attribute-value">"todayDate" class="attribute">format=class="attribute-value">"yyyy-MM-dd" class="tag">/>  
class="xml" style="DISPLAY: none" name="code"><s:class="tags" href="/tags/DATE.html" title=date>date name="todayDate" format="yyyy-MM-dd" />

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

 

翻了一下class="tags" href="/tags/STRUTS.html" title=struts>struts2的源码,和文档,找到一个办法。  com.opensymphony.xwork2.util.XWorkConverter

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

 

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

class="dp-highlighter">
class="bar">
class="tools"> Java代码 复制代码
    class="dp-j">
  1. className.replace(class="class="tags" href="/tags/STRING.html" title=string>string">'.'class="class="tags" href="/tags/STRING.html" title=string>string">'/') + class="class="tags" href="/tags/STRING.html" title=string>string">"-conversion.properties";  
class="java" style="DISPLAY: none" name="code">className.replace('.', '/') + "-conversion.properties";

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

 

ok,这个问题好解决了。

 

我的xwork-coversion.properties文件:

class="dp-highlighter">
class="bar">
class="tools"> Xml代码 复制代码
    class="dp-xml">
  1. class="attribute">java.util.Date=class="attribute-value">cn.jolstar.class="tags" href="/tags/STRUTS.html" title=struts>struts.type.DateTypeConverter  
class="xml" style="DISPLAY: none" name="code">java.util.Date=cn.jolstar.class="tags" href="/tags/STRUTS.html" title=struts>struts.type.DateTypeConverter

 我的DateTypeConverter代码:

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

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.class="tags" href="/tags/STRUTS.html" title=struts>struts2.util.StrutsTypeConverter;


/**
 * @author jolestar
 * 
 */
public class DateTypeConverter extends StrutsTypeConverter {

	private static final Logger log = Logger.getLogger(DateTypeConverter.class);
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	
	//暂时只考虑这几种日期格式
	public static final DateFormat[] ACCEPT_DATE_FORMATS = {
			new SimpleDateFormat(DEFAULT_DATE_FROMAT),
			new SimpleDateFormat("yyyy年MM月dd日"),
			new SimpleDateFormat("yyyy/MM/dd") };

	/**
	 * 
	 */
	public DateTypeConverter() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.class="tags" href="/tags/STRUTS.html" title=struts>struts2.util.StrutsTypeConverter#convertFromString(java.util.Map,
	 *      java.lang.String[], java.lang.Class)
	 */
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		if (values[0] == null || values[0].trim().equals(""))
			return null;
		for (DateFormat format : ACCEPT_DATE_FORMATS) {
			try {
				return format.parse(values[0]);
			} catch (ParseException e) {
				continue;
			} catch (RuntimeException e) {
				continue;
			}
		}
		log.debug("can not format class="tags" href="/tags/DATE.html" title=date>date class="tags" href="/tags/STRING.html" title=string>string:" + values[0]);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.class="tags" href="/tags/STRUTS.html" title=struts>struts2.util.StrutsTypeConverter#convertToString(java.util.Map,
	 *      java.lang.Object)
	 */
	@Override
	public String convertToString(Map context, Object o) {
		if (o instanceof Date) {
			SimpleDateFormat format = new SimpleDateFormat(
					DEFAULT_DATE_FORMAT);
			try {
				return format.format((Date) o);
			} catch (RuntimeException e) {
				return "";
			}
		}
		return "";
	}

}
 

 

 

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

 

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


http://www.niftyadmin.cn/n/1425656.html

相关文章

Hello,Flex!

刚开始学习Flex&#xff0c;不知从何下手。从以往学习C&#xff0c;Java的经验来看&#xff0c;任何一门语言都要理论与实践结合。就是找一本书&#xff0c;边看边练&#xff0c;这样学的快。如果有一位前辈教&#xff0c;那就更好了。不过目前看来&#xff0c;只能“自学成才”…

hbase数据导入导出

hbase数据导入 将本地文件(test.csv)上传到hdfs的根目录下,然后导入数据到hbase 1.本地写一个文件进行测试&#xff0c;文件名为test.csv,内容如下&#xff1a; 2.将文件上传到Hadoop 3.查看是否上传成功&#xff08;文件存在&#xff0c;表示成功&#xff09; 4.进入hbase s…

用MXML开发Flex应用-关于MXML

关于MXML 你可以用两种方式去编写Flex应用&#xff1a;MXML和ActionScript。MXML是一种用来设计编写用户接口组件的XML标记语言。你也可以用MXML去定义一个应用的非可视部分&#xff0c;比如访问服务器数据源和用户接口组件和服务器数据的绑定。 就像HTML一样&#xff0c;MXML用…

flex就业现状与学习标准分析

关于FLEX的学习,我有两个我自认为非常重要的观点: 1.FLEX很简单,很适合初学者入手,而且生动有趣,刚开始学习很有成就感; 2.FLEX更适合JAVA开发人员.不仅AS3语法上号称是简化版的JAVA,而且FLEX做为前端技术与强大的后端JAVA以及同时在企业应用上的配合堪称无双组合. 至于怎么样…

FLEX入门篇--------Alert提示框和双击事件

Alert的参考http://livedocs.adobe.com/flex/3/langref/mx/controls/Alert.html#effectSummaryAlert.show(text, title, flags, parent, closeHandler(关闭时触发的事件));flag : Alert.YES|Alert.NO|Alert.OK 简单的alert提示框说起. 先一起来看看官方文档的介绍: public stat…

自定义标签的分页使用(复杂)

分页使用1。<mytag:pagingDisplay />2。<tag> <name>pagingDisplay</name> <tagclass>nm.tag.DisplayTag</tagclass> <bodycontent>empty</bodycontent> <info> A demo </info> </tag>3。package nm.ta…

Pig安装部署与实例

安装包地址&#xff1a;https://mirrors.tuna.tsinghua.edu.cn/apache/pig/ 前提&#xff1a;Hadoop安装成功 pig安装部署&#xff1a; 1.将准备好的安装包上传到虚拟机rz 2.查看是否上传成功 3.解压缩 命令&#xff1a;tar xf pig-0.13.0.tar.gz 4.将解压缩后的文件移动到…

flash

FLASH导入到库的快捷键CTRLR 矢量渐变图导入flash在flash里面填充渐变的地方&#xff1f; EPS,AI格式请用Illustrator编辑 矢量&#xff08;很好很好很好&#xff09; http://www.veeqi.com/vector/people/ http://www.zcool.com.cn/vector/ 教学 http://www.7880.com/in…