我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

STRUTS2中的日期处理

1.Struts2中的日期的格式化:

在struts2中,提供了<s:date.../>标签,可以很方便地处理日期。有了这个标签,就可以随意地以各种方式来格式化日期,如指定日期格式为“dd/MM/yyyy hh:mm”,或者是自定义可读性强的日期格式“2 hours 14 minutes”,或者使用在properties文件中预先定义好日期格式。

s:date标签共有四个属性,id,name,format,nice;

(1)nice:

    可以设为true或者false,表示是否漂亮地显示日期,如果设置为true,那么将FORMAT属性将不会生效,日期将按以下规则显示:

i18n key default
struts.date.format.past {0} ago
struts.date.format.future in {0}
struts.date.format.seconds an instant
struts.date.format.minutes {0,choice,1#one minute|1<{0} minutes}
struts.date.format.hours {0,choice,1#one hour|1<{0} hours}{1,choice,0#|1#, one minute|1<, {1} minutes}
struts.date.format.days {0,choice,1#one day|1<{0} days}{1,choice,0#|1#, one hour|1<, {1} hours}
struts.date.format.years {0,choice,1#one year|1<{0} years}{1,choice,0#|1#, one day|1<, {1} days}

以上是默认的显示规则,可以在properties文件中修改。

(2)format:

    当nice="false"时,format属性将起作用,如:"yyyy-MM-dd hh:mm:ss",其中,y是年(year),M是月(Month),d是日(day),h是小时(hour,12小时制),H也是小时(hour,24小时制),m是分钟(minute),s是秒(second)。

(3)当format未设置时,将会使用默认的格式DateFormat.MEDIUM format

 

2.在struts2中使用日期控件:

    在struts2中,提供了日期控件,以方便地选择日期和时间,这是通过<s:datetimepicker.../>标签来实现的。该日期控件是通过DOJO来实现的,会在客户端生成一段JS代码,所以,要让该日期控件生效,需要在<head></head>中间加入以下代码:

   

xml 代码
  1. <s:head theme="ajax" />  

    这样,就可以在页面中使用<s:datetimepicker>标签了。如:

   

xml 代码
  1. <s:datetimepicker name="todayDate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/>  

    在页面中,就会显示出日期控件,其中displayFormat是要显示的日期格式。效果如下所示:

参考资料:http://struts.apache.org/2.x/docs/datetimepicker.html



posted on 2008-03-07 09:36 々上善若水々 阅读(3442) 评论(0)  编辑  收藏 所属分类: Struts2


只有注册用户登录后才能发表评论。


网站导航: