添加支持(即jar包)后的步骤为:
(1)编写资源文件
①英文资源文件,命名为application.en.properties
index.title=On NetBook Manager
index.userName=userName
index.passWord=passWord
index.submit=submit
index.reset=reset
index.language.english=English
index.language.zh=Chinese
②中文资源文件,命名为application.en.properties
index.title=在线图书管理系统
index.userName=用户名
index.passWord=密码
index.submit=提交
index.reset=重置
index.language.english=英文
index.language.zh=中文
并对中文资源文件进行转换,其格式如下:
native2ascii e:\application.en.properties f:\application.en.properties
得到转码文件为:
index.title=\u5728\u7EBF\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF
index.userName=\u7528\u6237\u540D
index.passWord=\u5BC6\u7801
index.submit=\u63D0\u4EA4
index.reset=\u91CD\u7F6E
index.language.english=\u82F1\u6587
index.language.zh=\u4E2D\u6587
(2)加载资源文件
资源文件的加载通过Struts-config.xml文件来配置,并从web应用的WEB-INF/classes路径开始加载。
(3)使用Bean标签显示国际化信息
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
.......
<body>
<table border="1" width="300">
<tr>
<td colspan="2" align="center">
<bean:message key="index.title"/>
<a href="languageAction.do?en=0">
<bean:message key="index.language.english"/>
</a>
<a href="languageAction.do?en=1">
<bean:message key="index.language.zh"/>
</a>
</td>
</tr>
<tr>
<td><bean:message key="index.userName"/></td>
<td><input name="textfield" type="text"/></td>
</tr>
<tr>
<td><bean:message key="index.passWord"/></td>
<td><input name="textfield2" type="text"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="<bean:message key="index.submit"/>"/>
<input type="reset" name="reset" value="<bean:message key="index.reset"/>"/>
</td>
</tr>
</table>
</body>
(4)创建业务逻辑控制器Action,命名为LanguageAction.java
package com.mstf.action;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LanguageAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//获取页面选择的en
int en=Integer.parseInt(request.getParameter("en"));
if(en==0){
//<bean:message/>标签将根据保存在session中地Locale对象决定需要加载的语言,此处为英文
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
}
else{
request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.CHINA);
}
//返回index.jsp页面
return mapping.findForward("index");
}
}
(5)配置Struts-config.xml文件
<struts-config>
....
<action-mappings>
<action path="/languageAction" type="com.mstf.action.LanguageAction">
<forward name="index" path="/index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="resource.application" />
</struts-config>
发布、并运行