1. 加载国际化资源文件的方式:
(1) 全局资源文件:配置struts.custom.i18n.resources常量,这种方式加载国际化资源文件,Struts2应用可以在所有地方获取国际化资源;
(2) 包范围资源文件:在包的根路径下建立多个文件名为package_language_country.properties的文件,这种方式加载国际化资源文件,应用中处于该包下的所有Action都可以访问该资源文件。
(3) Action范围资源文件:在Action类文件所在的路径建立多个名为ActionName_language_country.properties的文件,这种方式加载国际化资源文件,这系列的资源文件只能有该Action来访问。
(4) 临时指定资源文件:在JSP页面中借助Struts2的标签<s:i18n .../>。
2.访问国际化消息的方式:
(1) 在JSP页面中输出国际化消息,使用Struts2的标签<s:text .../>,该标签的name属性指定了国际化资源文件中的key;
(2) 在表单元素的Lable里输出国际化消息,为该表单标签指定一个key属性,该key指定了国际化资源文件中的key;
(3) 在Action中访问国际化消息,使用ActionSupport类的getText方法,该方法接收一个name参数,该参数指定了国际化资源文件中的key。
3. 一个简单示例:
提供两份国际化资源文件msg_zh_CN_properties(需要用native2ascii工具处理)和msg_en_US_properties:
loginPage=登录页面
successPage=成功页面
errorPage=错误页面
successTip=欢迎您,{0}~
failTip={0},对不起,您不能登录~
timeTip=您好,现在是{0}~
username=用户名
password=密码
login=登录
loginPage=LoginPage
successPage=SuccessPage
errorPage=ErrorPage
successTip=Welcome, {0}~
failTip={0}, sorry, you can not login~
timeTip=Hello, now it is {0}~
username=Username
password=Password
login=Login
在struts.xml中配置struts.custom.i18n.resources常量:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 指定国际化资源文件的baseName为msg -->
<constant name="struts.custom.i18n.resources" value="msg"/>
<package name="login" extends="struts-default">
<action name="loginAction" class="com.huey.action.LoginAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
在JSP中访问国际化消息,下面是登录页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- 使用s:text标签输出国际化消息 -->
<title><s:text name="loginPage" /></title>
</head>
<body>
<s:form action="loginAction" method="post">
<!-- 使用表单标签输出国际化消息 -->
<s:textfield name="username" key="username"/>
<s:password name="password" key="password"/>
<s:submit key="login"/>
</s:form>
</body>
</html>
在Action中访问国际化消息:
package com.huey.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @version 2013-08-18
* @author huey2672
*
*/
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1302557649514221327L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
ActionContext cxt = ActionContext.getContext();
if (username.equals("huey") && password.equals("123")) {
// 取得国际化资源,带占位符的用字符串数组或List填充
String successTip = getText("successTip", new String[]{getUsername()});
cxt.put("tip", successTip);
return SUCCESS;
}
String failTip = getText("failTip", new String[]{getUsername()});
cxt.put("tip", failTip);
return ERROR;
}
}
其他视图资源(success.jsp和error.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="successPage" /></title>
</head>
<body>
<s:property value="tip" /><br/>
<jsp:useBean id="time" class="java.util.Date" scope="page"></jsp:useBean>
<!-- 带占位符的国际化消息,用s:param标签来填充 -->
<s:text name="timeTip">
<s:param>${time}</s:param>
</s:text>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="errorPage" /></title>
</head>
<body>
${requestScope.tip}<br/>
</body>
</html>