Struts 2.0 国际化

本文介绍了Struts框架中如何配置和使用国际化资源文件。详细解释了资源文件的查找顺序及三种访问方式,并提到了I18nInterceptor的作用及配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

全局资源文件

可以在struts.properties中配置全局资源文件,示例如下:

java 代码
  1. struts.custom.i18n.resources = application  

查找顺序

struts按照如下顺序查找资源文件
        1.ActionClass.properties 
        2.BaseClass.properties (all the way to Object.properties) 
        3.Interface.properties (every interface and sub-interface) 
        4.ModelDriven's model (if implements ModelDriven), for the model object repeat from 1 
        5.package.properties (of the directory where class is located and every parent directory all the way to the root directory) 
        6.search up the i18n message key hierarchy itself 
        7.global resource properties

访问资源

有三种方式访问资源文件
使用getText

xml 代码
  1. <s:property value="getText('some.key')" />  

使用text标记

xml 代码
  1. <-- Fourth Example -->  
  2. <s:text name="some.key" />  
  3.   
  4. <-- Fifth Example -->  
  5. <s:text name="some.invalid.key" >  
  6.    The Default Message That Will Be Displayed   
  7. </s:text>  

使用I18n标记

xml 代码
  1. <s:i18n name="some.package.bundle" >  
  2.      <s:text name="some.key" />  
  3. </s:i18n>  

I18n Interceptor

        此拦截器的主要功能是支持动态改变会话的Local。实现原理是会拦截每一个请求参数,如果请求参数包含request_locale键(可配置),拦截 器会将其值解析出Local对象储存到会话的属性,缺省健值为WW_TRANS_I18N_LOCALE(可配置),然后会将此Local传递给当前 Action实例。
        以便Action实现的LocalProvider和TextProvider接口立即使用此Local获取资源文件。

        注意:资源属性文件应该使用ISO8859-1编码,涉及中文字符使用16进制编码,Eclipse有专门处理Properties文件的插件可以简化此操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值