Struts2 中国际化的配置与使用

Struts2 中国际化的配置与使用

1. 资源文件的导入

通过配置 struts.properties 或者 struts.xml 来完成

struts.properties 的配置为:

struts.custom.i18n.resources=config.i18n.ApplicationResources-cn, config.i18n.ApplicationResources-en

多个资源文件以逗号【,】进行分割, config.i18n.ApplicationResources-cn 包含了资源文件的路径及文件名称,但不包括文件后缀名。

struts.xml 的配置为: 

<constant name="struts.custom.i18n.resources" value="config.i18n.ApplicationResources-cn,config.i18n.Global-cn"></constant>

这两种配置方法其实是一样的,都是通过对资源文件的配置,然后在服务器启动的时候,将资源文件中的内容加载到Struts2的上下文中去。


2. Action中如何调用资源文件中的内容

在Struts2  中,因为所有的Action都继承了 ActionSupport.java 类,所以也就继承了 ActionSupport 类中的一些方法

addActionError(getText("error_code",new String[]{}));

addActionMessage(getText("message_code",new String[]{}));

其中的 getText() 方法就可以获取到 *.properties 文件中的值, 后面的 new String[]{} 可以传递一些参数到资源文件中,资源文件对应的写法如下:

warning.category.code.existed=分组编码 [{0}] 已经存在


3. 页面如何显示后台返回的错误信息

Struts2 的 s 标签可以输出后台Action返回的值,如下:

<s:actionmessage />
<s:actionerror  />
<s:fielderror />

4. 页面上如何调用资源文件中的内容

页面可以通过 Struts2 的标签来调用资源文件中的内容,如下:

<s:text name="label.menu.code"/>

其中 label.menu.code 对应 properties 文件中的key值。


附:国际化资源文件内容格如下:

#warning
warning.category.code.notnull=分组编码不能为空
warning.category.code.existed=分组编码 [{0}] 已经存在

 

 

posted @ 2012-10-15 13:49  戴眼镜的码农  阅读(668)  评论(0编辑  收藏  举报