4.3 Struts 2的国际化支持
前面已经介绍了Java国际化的原理和方法,读者知道,Java的国际化需要一个Locale和一个资源包就能够实现国际化。资源包可以是资源文件也可以是资源类文件。
Struts 2的国际化是建立在Java国际化的基础之上的,也是使用资源包的方式,通过getBundle()方法来寻找指定Locale相关联的资源包,再从资源包文件中查找指定Key所对应的国际化资源信息。
Struts 2框架的底层国际化与Java国际化是一致的,作为一个良好的MVC框架,Struts 2将Java的国际化功能进行了封装和简化,开发者使用起来会更加简单快捷。
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,对应的class为com.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文件中定义。
假设要定义配置一个basename为globalMessages的struts.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
<% @ 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来获得国际化信息。有关标签库的知识,后面将会详细讲解,在这里读者只需要简单了解。