一旦开发者指定了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所示。
<%@ 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所示。
<%@ 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所示。
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相关联的国际化信息资源。