1, 新建要实现国际化的页面
<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-HTML" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>资源国际化</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function doSubmit(op){
document.getElementById("op").value = op;
document.getElementById("myForm").submit();
}
</script>
</head>
<body>
<a href="locale.do?locale=zh">Chinese</a>
<a href="locale.do?locale=en">English</a>
<span style="font-size:18px;"><bean:message key="title"/></span>
<form action="cal.do" method="post" name="myForm" id="myFrom">
<input type="hidden" name="op"><br/>
<bean:message key="num1"/><input type="text" name="num1"><br/>
<bean:message key="num2"/><input type="text" name="num2"><html:errors property="error2"/><br/>
<bean:message key="result"/><input type="text" name="result" value="${res }"><br/>
<input type="submit" name="btn" value="<bean:message key='add'/>" onclick="doSubmit('doAdd')">
<input type="submit" name="btn" value="<bean:message key='sub'/>" onclick="doSubmit('doSub')">
<input type="submit" name="btn" value="<bean:message key='mul'/>" onclick="doSubmit('doMul')">
<input type="submit" name="btn" value="<bean:message key='div'/>" onclick="doSubmit('doDiv')"><br/><br/>
<bean:message key="name"/>
</form>
</body>
</html:html>
2, 新建名为LocaleAction的Action
package com.aptech.web.action;
import java.util.Locale;
import javax.Servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 LocaleAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String locale = request.getParameter("locale");//获取参数
Locale loc = new Locale(locale);//创建对象
HttpSession session = request.getSession();
session.setAttribute(Globals.LOCALE_KEY, loc);
return mapping.findForward("input");
}
}
3, action在struts-config.xml中的配置
<action
attribute="calForm"
name="calForm"
input="/cal.jsp"
path="/locale"
scope="request"
type="com.aptech.web.action.LocaleAction">
<forward name="input" path="/cal.jsp" />
</action>
4, 新建英文(ApplicationResources_en.properties)和中文(ApplicationResources_zh.properties)资源文件
以英文(ApplicationResources_en.properties)为例(中文的需要编码后才可以):
add = add
div = div
mul = mul
name = by jie zhang
num1 = the First Number
num2 = the Second Number
result = result:
sub = subtitle = caculator
运行效果图:
1,默认或者中文状态下
2,英文状态下