struts2格式化日期和时间

来源:互联网 发布:淘宝品牌手表 编辑:程序博客网 时间:2024/05/16 11:09

 本文参考自:http://www.360doc.com/showWeb/0/0/8042943.aspx

 

struts2 中的默认的日期输出并不符合我们的中文日常习惯.以下是我知道的在struts2中进行日期格式化输出的几种方式.

1.利用 <s:date>进行格式化.如:
<s:date name="birthday" format="yyyy-MM-dd"/>就是将属性birthday按yyyy-MM-dd的格式进行输出.

2.利用<s:param>进行格式化.如:
<s:textfield name="birthday" >
      <s:param name="value"><s:date name="birthday" format="yyyy-MM-dd"/></s:param>
</s:textfield>
或者<input type="text" value="<s:date name="birthday" format="yyyy-MM-dd" />" />

还有一种是通过国际化的方式来进行:
首先在struts.properties中配置struts.custom.i18n.resources=globalMessages
然后在globalMessages_zh_CN.properties和globalMessages_en_US.properties文件中添加如下内容:
global.datetime = {0, date, yyyy-MM-dd HH:mm:ss}
global.date = {0, date, yyyy-MM-dd}
最后在页面中使用下面的语句就可以实现日期的格式化输出了.
<s:textfield name="birthday" value="%{getText('global.date',{birthday})}"/> --golbal.date样式
<s:textfield name="birthday" value="%{getText('global.datetime',{birthday})}"/> --global.datetime样式
 
例子:
1、<s:date name="#request.sysUser.moditime" format="yyyy-MM-dd"/>
 2、 <input type="text" name="moditime1"  value="<s:date format="yyyy-MM-dd" name="#request.sysUser.moditime"/>">
 3、 <s:textfield name="#request.sysUser.moditime" >
        <s:param name="value"><s:date name="#request.sysUser.moditime" format="yyyy-MM-dd"/></s:param>
  </s:textfield>
4、   <s:textfield name="moditime" value="%{getText('global.format.datetime',{#request.sysUser.moditime})}" cssClass="inputd_readOnly" />
另:
在资源文件中定义带参数的属性如label.hello=hello {0}时
页面可通过如下方式赋值“callan”
<s:text name="label.hello">
    <s:param>callan</s:param>
</s:text>
显示结果为hello callan
原创粉丝点击