struts2配置和Java国际化和JS国际化

1 WebContent/WEB-INF/web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter>
    	<filter-name>struts-cleanup</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  	</filter>
  	
  	<filter-mapping>
    	<filter-name>struts-cleanup</filter-name>
    	<url-pattern>/*</url-pattern>
  	</filter-mapping>

</web-app>

2 src/struts.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
	<!--
		用于标示struts的国际化,规定国际化文件要以globalMessage做为开头,如:“globalMessage_zh_CN.properties”。
		
		以下说明为有精力的同学看(可以不看):
		也可以不要这行代码,采取属性文件的方式。具体做法是:先删除这行代码,然后在项目的src下
		建一个属性文件,名为struts.properties(注:必须取这个名字),在属性文件中键入:
		struts.custom.i18n.resources=globalMessage保存即可。
		这个等号后面的"globalMessage"是随意取的,只要取的名字与国际化属性文件的名字
		相对应即可,例如:若写"struts.custom.i18n.resources=Myi18n"那么国际化文件
		的名字就要以Myi18n开头,如中文国际化属性文件名字就应该为"Myi18n_zh_CN.properties".
	-->
	<!-- value=message.globalMessage 对应着 资源文件路径 src/message/globalMessage*.properties -->
	<constant name="struts.custom.i18n.resources" value="message.globalMessage"></constant>

	<!-- 对应com.test.action包中的loginAction类 -->
	<package name="test" namespace="/" extends="struts-default">
		<action name="loginAction" class="com.test.action.LoginAction">
			<result name="success">welcome.jsp</result>
			<result name="login">login.jsp</result>
		</action>
	</package>
</struts>

3 JAVA国际化资源配置 src/message/

   globalMessage_en_US.properties

login.title=login title
login.firstname=firstname
login.lastname=lastname
login.age=age
login.submit=submit
login.info=please write {0} and {1}
login.welcome=welcome you

   globalMessage_zh_CN.properties

login.title=用户登录
login.firstname=姓
login.lastname=名
login.age=年龄
login.submit=提交
login.info=请输入 {0}和{1}
login.welcome=欢迎你

4 JS国际化 WebContent/js/locale

   jsLocale.jsp js资源加载jsp

<%@ page language="java" import="java.util.*" 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>
<title>Insert title here</title>

<script type="text/javascript" src="<%=request.getContextPath() %>/js/locale/locale_<%=request.getLocale().toString() %>.js"></script>

<script type="text/javascript">
	//js key获取函数
	function getJsLocale(key)
	{
		if (typeof(jsLocale) != 'undefined')
		{
			if (typeof(jsLocale[key]) != 'undefined')
			{
				return jsLocale[key];
			}
			else
			{
				return key;
			}				
		}
		else
		{
			return key;
		};
	}
</script>

</head>
<body>

</body>
</html>

   locale_en_US.js

var jsLocale = 
{
	name : "sky",
	sex  : "man",
	info : "locale from js"
};
 

   locale_zh_CN.js

var jsLocale = 
{
	name : "李晓峰",
	sex  : "男",
	info : "js国际化"
};

  5 后台登陆action

   LoginAction

package com.test.action;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  姓名 工号
 * @version  [版本号, 2012-10-9]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class LoginAction extends ActionSupport
{

	@Override
	public String execute() throws Exception
	{
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	/**
	 * 
	 * 手动转换Locale
	 * @return
	 * @throws Exception
	 * @see [类、类#方法、类#成员]
	 */
	public String i18n() throws Exception 
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String language = request.getParameter("language");
		String country = request.getParameter("country");
		
		Locale locale = new Locale(language, country);
		ActionContext.getContext().setLocale(locale);
		
		getResource();
		
		return "login";
	}
	
	/**
	 * 测试手动获取资源
	 * @see [类、类#方法、类#成员]
	 */
	public void getResource()
	{
		Locale locale = new Locale("zh","CN");
		ResourceBundle bundle = ResourceBundle.getBundle("message.globalMessage",locale);
		String name = bundle.getString("login.submit");
		String name1 = bundle.getString("login.info");
		
		Object[] os = {"infi","th000"};
		
		String name11 = MessageFormat.format(name1, os);
		
		String name3 = getText("name");
		System.out.println(name);
		System.out.println(name11);
		System.out.println(name3);
	}
}

  6 国际化测试JSP页面 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title><s:text name="login.title"></s:text></title>
	</head>
	<body>
		<s:text name="login.title"></s:text>
		
		<br>
		<hr />
		<!-- 下面是手动地点击想要的语言。其中规定了,超联接要提交到的方法(i18n),提交时一起传入相应的语言及国家的代码值 -->
		<a href="<%=path%>/loginAction!i18n.action?language=en&country=US">English</a>|
		<a href="<%=path%>/loginAction!i18n.action?language=zh&country=CN">中文</a>
		<br>

		<!-- 以下代码中label="%{getText('login.firstname')}"是读取属性文件(如:globalMessage_en_US.properties)中的名为"login.firstname"的属性的值。 -->
		<form action="<%=path%>/loginAction" method="post">
			<table align="left">
				<s:textfield name="firstname" label="%{getText('login.firstname')}"></s:textfield>
				<s:textfield name="lastname" label="%{getText('login.lastname')}"></s:textfield>
				<s:textfield name="age" label="%{getText('login.age')}"></s:textfield>
				<s:submit value="%{getText('login.submit')}"></s:submit>			
				<s:text name="login.info">
					<s:param value="%{getText('login.firstname')}"/>
					<s:param value="%{getText('login.lastname')}"/>
				</s:text>			
			</table>
		</form>
	</body>
</html>

   7 国际化测试页面 java国际化 + js国际化

    welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
			
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title><s:text name="login.welcome"></s:text></title>
		<jsp:include page="/js/locale/jsLocale.jsp" />
		<script type="text/javascript">
			function init()
			{
				document.getElementById("locale").innerHTML = getJsLocale("info");
			}
		</script>
	</head>
	<body onload="init()">		
		<s:text name="login.welcome"></s:text>
		<div id="locale"></div>
	</body>
</html>

   8 strut2 架包

   commons-fileupload-1.2.1.jar

   commons-io-1.3.2.jar

   commons-logging-1.1.jar

   freemarker-2.3.13.jar

   junit-3.8.1.jar

   ognl-2.6.11.jar

   servlet-api.jar

   struts2-core-2.1.6.jar

   xwork-2.1.2.jar

猜你喜欢

转载自yjshengshe.iteye.com/blog/1694742