Struts 2国际化

一般的框架的国际化都是通过为不同的语言环境提供不同的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值