日期:2014-05-16  浏览次数:20376 次

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>

?<