Struts 2国际化

来源:互联网 发布:短网址赚钱源码 编辑:程序博客网 时间:2024/06/05 09:24
一般的框架的国际化都是通过为不同的语言环境提供不同的properties文件来实现的,Struts2也不例外。


Struts2的国际化用的properties文件可以用如下几种方式提供:
1、用与action的class文件同名的properties文件,比如为HelloWorldAction.java的action配置一个HelloWorld.properties的属性文件;
2、用一个package目录或是父package目录下的properties文件;
3、定义一个全局的properties文件。这个需要在Struts.xml的文件中增加如下配置:
<constant name="struts.custom.i18n.resources" value="global" /> 用来告诉Struts frame这是一个全局properties文件;


以下是一些注意事项:
a、properties文件一般放在class文件的package下;
b、中文在properties文件中需要转义成utf-8编码,可以使用jdk的自带工具:
native2ascii -encoding UTF-8 RegistAction_zh.properties RegistAction_zh_CN.properties
c、Struts2的属性文件是要在执行完action的execute、input、error等方法后才被“找到”的,所以你只是想直接访问一个简单的页面,并在其中使用properties文件中的值,你得这么来:
<action name="index">
   <result>/index.jsp</result>
 </action>
d、可以在struts.xml中配置系统的相应语言环境,如(中文、utf编码):
   <constant name="struts.locale" value="zh_CN" />
      <constant name="struts.i18n.encoding" value="UTF-8" />  //这个控制乱码用的
e、上面提供了三种提供properties文件的方式,Struts frame 在寻找相应的properties文件中的属性的时候,遵循如下规则:
Struts 2 will look for a property key of contact in all the property files starting with the property file that matches the Action class, then in the property files that are in the package hierarchy of the Action class, and then in any property files specified in struts.xml.


还有,就是Struts的乱码情况比较多,最简单的解决办法就是把各种编码都设置为utf编码咯。比如,页面编码格式、Struts的语言环境编码格式、web容器的编码格式、httpRequest的请求编码格式、httpResponse的响应编码格式、数据库的编码格式等。


这些呢在Struts 2的源码里就包含了HTML格式的说明文档的了

0 0