应用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来获得国际化信息。有关标签库的知识,后面将会详细讲解,在这里读者只需要简单了解。

(4) 同样,success.jsp也使用了标签库,如代码4.10所示。

代码4.10  注册成功界面success.jsp

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

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

< html >

< head >

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

< s:head  />

</ head >

< body >

< table >

< h2 >< s:text  name ="username" /> < s:property   value ="username"   /></ h2 >

< h2 >< s:text  name ="password1" /> < s:property   value ="password1"   /></ h2 >

< h2 >< s:text  name ="birthday" /> < s:property   value ="birthday"   /></ h2 >

</ table >

</ body >

</ html >

 

上面两个JSP用户视图,所有的显示内容都使用了国际化信息,可以根据用户不同的语言与区域配置,来显示相应的国际化内容。

那么,用户视图可以访问国际化资源,在Action中可以访问吗?答案是可以的,前面已经介绍过了,Struts 2提供了一个ActionSupport工具类,开发自己的Action,只需要继承该类就可以。在该类的API文档中,可以发现,该类提供了一个getText(String aTextName)方法,该方法根据资源文件中的key值来返回一个国际化资源信息,如果找不到则为null

(5) 在本示例的Action中,会调用getText(String aTextName)方法,获得资源文件中的国际化信息,并在控制台中打印出来,如代码4.11所示。

代码4.11  国际化业务控制器reg

package  ch4;

import  java.util.Date;

import  com.opensymphony.xwork2.ActionSupport;

public   class  Reg  extends  ActionSupport  {

    
//定义用户名属性

    
private String username;

    
//定义处理信息:注意与http中的msg名称不同

    
private String mymsg;

    
//定义密码属性

    
private String password1;

    
//定义确认密码

    
private String password2;

    
//定义生日属性

    
private Date birthday;

    
public String execute() throws Exception {

        
//判断用户输入参数

        
if (username != null && getPassword1().equals(getPassword2())

                
&& !getUsername().trim().equals("")) {

            
//打印国际化信息

            System.out.println(getText(
"username"+ ":" + username);

            System.out.println(getText(
"password1"+ ":" + password1);

            System.out.println(getText(
"birthday"+ ":" + birthday);

            
return SUCCESS;

        }
 else {

            
return INPUT;

        }


    }


    
//getter和setter方法

    
public String getUsername() {

        
return username;

    }


    
public void setUsername(String username) {

        
this.username = username;

    }


    
public String getMymsg() {

        
return mymsg;

    }


    
public void setMymsg(String mymsg) {

        
this.mymsg = mymsg;

    }


    
public String getPassword1() {

        
return password1;

    }


    
public void setPassword1(String password1) {

        
this.password1 = password1;

    }


    
public String getPassword2() {

        
return password2;

    }


    
public void setPassword2(String password2) {

        
this.password2 = password2;

    }


    
public Date getBirthday() {

        
return birthday;

    }


    
public void setBirthday(Date birthday) {

        
this.birthday = birthday;

    }


}


 

 (6)运行该应用,在中文、英文的语言与区域配置环境中,注册界面分别如图4.7和图4.8所示。当设置语言与区域选项为“简体中文”时,Tomcat控制台会打印出中文信息:

注册用户名:pla

密码:123456

生日:Wed Nov 28 00:00:00 CST 2007

当设置语言与区域选项为“英语美国”时,Tomcat控制台会打印出英文信息:

信息: Detected AnnotationActionValidatorManager, initializing it...

Your Name:pla

Password:123456

Birthday:Sun Oct 28 00:00:00 CST 2007

         

4.7  中文注册界面        

                

  4.8  英文注册界面

分别在中文和英文配置下,输入“pla”等注册信息,单击“注册”按钮或者“Reg!”按钮,结果分别如图4.9和图4.10所示,界面显示的所有元素都使用国际化信息输出。

           

   4.9  注册成功中文界面                      

4.10  注册成功英文界面

Action中可以使用ActionSupport类提供的getText(String aTextName)方法来获得国际化信息,证实了前面所讲的国际化拦截器将用户请求参数中的Locale值存入了当前session中,Action正是获得了与Locale相关联的国际化信息资源。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值