转: 应用Struts 2 的国际化支持特性(7)上

4.3  Struts 2的国际化支持

前面已经介绍了Java国际化的原理和方法,读者知道,Java的国际化需要一个Locale和一个资源包就能够实现国际化。资源包可以是资源文件也可以是资源类文件。

Struts 2的国际化是建立在Java国际化的基础之上的,也是使用资源包的方式,通过getBundle()方法来寻找指定Locale相关联的资源包,再从资源包文件中查找指定Key所对应的国际化资源信息。

Struts 2框架的底层国际化与Java国际化是一致的,作为一个良好的MVC框架,Struts 2Java的国际化功能进行了封装和简化,开发者使用起来会更加简单快捷。

4.3.1  配置资源文件

Struts 2强调的是各个组件之间的松散耦合,而各个组件之间都是通过配置文件来实现相互关联和交互的。Struts 2框架的国际化也是如此。

Struts 2框架提供了多种加载国际化资源文件的方式,其中最常用的就是读者前面已经熟悉的加载资源文件的方式来实现国际化。Struts 2框架加载资源文件一般都是通过常量设置来完成的。Struts 2框架的默认配置文件struts-deault.xml中已经定义了国际化拦截器,部分内容如下:

 

<! —定义拦截器 -- >

< interceptors >

<! —定义拦截器alias -- >

 
< interceptor  name ="alias"  class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />

<! —定义拦截器autowiring -- >

< interceptor  name ="autowiring"  class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />

< interceptor  name ="chain"  class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />

< interceptor  name ="conversionError"  class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />

< interceptor  name ="cookie"  class ="org.apache.struts2.interceptor.CookieInterceptor" />

< interceptor  name ="createSession"  class ="org.apache.struts2.interceptor.CreateSessionInterceptor"   />

< interceptor  name ="debugging"  class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor"   />

< interceptor  name ="externalRef"  class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />

< interceptor  name ="execAndWait"  class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />

<! —定义异常拦截器-- >

< interceptor  name ="exception"  class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />

<! —定义文件上传拦截器-- >

< interceptor  name ="fileUpload"  class ="org.apache.struts2.interceptor.FileUploadInterceptor" />

<! —定义国际化拦截器-- >

< interceptor  name ="i18n"  class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />

<! —定义日志拦截器-- >

< interceptor  name ="logger"  class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />

< interceptor  name ="modelDriven"  class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />

… …

 

可见,该拦截器为i18n,对应的classcom.opensymphony.xwork2.interceptor.I18n Interceptor,有兴趣的读者可以参考其API文档。

I18nInterceptor是一个拦截器类,该拦截器在Action执行处理之前执行,该拦截器掌管着当前用户请求Session中的Locale相关数据。该拦截器会在用户参数中查找一个特殊的参数值,使用该参数来设置当然的Locale信息,这就意味着开发者可以动态地修改当前session中的Locale值,这在开发国际化应用中非常有意义,开发者可以在处理用户请求过程中任何一个节点来改变Locale值,这样就能够动态改变程序的语言和区域的相关信息,实现完善的国际化功能。

例如,使用默认的拦截器参数,假设一个用户请求为foo.action?request_locale=en_US,那么英语(语言)和美国(国家)将会被保存到请求的session中,在以后的请求处理过程中会被使用。该拦截器有下面两个参数:

 parameterName:可选参数,即用户请求参数中包含Locale值的参数名称,默认为request_locale

 attributeName:可选参数,session中被选用的Locale值的key,默认为WW_TRANS_I18N_LOCALE

配置资源文件常量,即配置Struts 2框架的struts.custom.i18n.resources常量,该常量定义了Struts 2框架全局国际化资源文件的basename

如果开发者需要在项目应用中提供国际化功能,则需要指定struts.custom.i18n.resources常量值。配置struts.custom.i18n.resources常量,可以在属性文件struts.properties中定义,也可以在配置文件struts.xml或者web.xml文件中定义。

假设要定义配置一个basenameglobalMessagesstruts.custom.i18n.resources常量,可以在struts.properties文件中做如下配置:

#在属性文件中定义basename

struts.custom.i18n.resources=globalMessages

也可以在struts.xml文件中配置basename

I18N资源文件为globalMessages

    <constant name="struts.custom.i18n.resources"   value="globalMessages" />

同样,也可以在web.xml文件中定义:

… …

<!--定义struts.custom.i18n.resources常量-->

        <init-param>

            <param-name>struts.custom.i18n.resources</param-name>

            <param-value>globalMessages</param-value>

        </init-param>

… …

配置好Struts 2框架的国际化资源文件的basename后,开发者就可以按照basename_language_country.properties的命名规则来建立不同语言环境的资源文件了,当然,如果是非西欧字符集,则需要使用native2ascii转换工具转换为Unicode编码即可。

 

4.3.2  Struts 2国际化应用

一旦开发者指定了struts.custom.i18n.resources常量,即指定了国际化资源文件的basename,那么就可以开发国际化应用了。下面以一个注册的示例来演示Struts 2框架的国际化功能。值得注意的是,该示例与前面章节的用户注册是有所差别的。注册用户Action关系图如图4.6所示。

 

4.6  注册应用Action关系图

(1) 建立中文和英文的资源文件,globalMessages_en_US.properties内容如代码4.7所示。

代码4.7  英文资源文件globalMessages_en_US.properties

#英文资源文件内容

HelloWorld=Hello World!

user=username

pass=password

username=Your Name

password1=Password

password2=confirm Password

birthday=Birthday

regpage=Reg Page

errpage=ERROR Page

successlogin=Welcom

falselogin=Sorry!You can't log in

regsuccess=OK,You reg success!

regfalse=Sorry! You Reg False!

regbutton=Reg!

  (2)globalMessages_zh_CN.properties内容如代码4.8所示。

代码4.8  中文资源文件globalMessages_zh_CN.properties

#简体中文资源文件内容

HelloWorld=你好,世界!

name=用户名称

pass=用户密码

username=注册用户名

password1=密码

password2=确认密码

birthday=生日

regpage=注册界面

errpage=错误界面

successlogin=登录成功

falselogin=登录失败

regsuccess=注册成功

regfalse=对不起,注册失败!

regbutton=注册

  (3)globalMessages_zh_CN.properties文件为中文资源文件,该文件在使用前,必须使用native2ascii转换工具转换。接下来建立输入界面reg.jsp,如代码4.9所示。

代码4.9  输入界面reg.jsp

 

<% @ page contentType="text/html;charset=UTF-8" language="java"  %>

<% @ taglib prefix="s" uri="/struts-tags"  %>

< html >

< head >

< title >< s:text  name ="regpage" /></ title >

< s:head  />

</ head >

< body >

< table >

< s:form  id ="id"  action ="Reg" >

    
<! —使用key来加载国际化资源信息 -- >

    
< s:textfield  name ="username"  key ="username" />

    
< s:password  name ="password1"  key ="password1" />

    
< s:password  name ="password2"  key ="password2" />

    
< s:datetimepicker  name ="birthday"  key ="birthday"  displayFormat ="yyyy-MM-dd" />

    
< s:submit  key ="regbutton" />

</ s:form >

</ table >

</ body >

</ html >

 

reg.jsp中使用了标签库来访问资源文件,<s:text/>是显示静态文本,该标签中可以使用key属性来输出国际化信息。Form元素的标签也可以使用key来获得国际化信息。有关标签库的知识,后面将会详细讲解,在这里读者只需要简单了解。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值