1.在struts.properties中插入struts.custom.i18n.resources=globalMessages
2.src目录建立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}
3.页面调用部分:
<s:textfield name="pi.applyDate" value="%{getText('global.date',{pi.applyDate})}"/>
原理说明:
struts2的资源文件如同其验证文件一样,是允许分散和集中的。使用范围在于Tags和field error中描述信息的查找。整个搜索顺序如下:
- ActionClass.properties 或者ActionClass_xx_XX.properties (xx_XX可以为zh_CN等)
- BaseClass.properties
- Interface.properties
- ModelDriven's model (如果实现了getModel())的类文件
- package.properties (包括superclass的package)
- search up the i18n message key hierarchy itself
- global resource properties