一.
gjl_en_US.properties
welcome=welcome here
gjl_zh_CN.properties
welcome={0}/uFF0C/u6B22/u8FCE/u4F60 {1}
struts.xml
<constant name="struts.custom.i18n.resources" value="gjl" />
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.action.extension" value="do,action"/> <constant name="struts.custom.i18n.resources" value="gjl" /> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.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> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <s:text name="welcome" > <s:param >123</s:param> <s:param >gjl</s:param> </s:text> </body> </html>
补充内容
struts2国际化 -struts2学习笔记 -hxzon -grule -gcode
1,在struts配置文件中添加<constant name="struts.custom.i18n.resources" value="messageResource" />资源文件保存在classes目录下。2,访问资源JSP中使用<s:text name="" />标签Action中继承ActionSupport,使用getText(key)表单元素的Label可以使用key属性。甚至可以<s:textfield name="name" label="%{getText('UserName')}"/>----------------
3,带占位符,如welcome=hello,{0}!JSP中在<s:text>中嵌套多个<s:param value="" ></s:param>子标签。甚至更简单的,<s:text name="validation.required" value0="User Name"/>,使用value0,value1,value2等属性。Action中使用getText(String key,List args)或者getText(String key,String[ ] args)4,除此之外,还有一种替代占位符的方式,即在资源文件中使用表达式,如welcome=hello,${username}!=========================-----------------不同范围的资源文件包范围,package_语言_国籍.properties,注意不是包名。action范围,action类名_语言_国籍.properties临时指定,使用<s:i18n name="tmp" >做父标签。它的子标签将临时使用tmp_zh_CN.properties。--------------加载顺序资源文件查找顺序之所以说Struts 2.0的国际化更灵活是因为它可以能根据不同需要配置和获取资源(properties)文件。在Struts 2.0中有下面几种方法:
使用全局的资源文件,方法如上例所示。这适用于遍布于整个应用程序的国际化字符串,它们在不同的包(package)中被引用,如一些比较共用的出错提示; 使用包范围内的资源文件。做法是在包的根目录下新建名的package.properties和package_xx_XX.properties文件。这就适用于在包中不同类访问的资源; 使用Action范围的资源文件。做法为Action的包下新建文件名(除文件扩展名外)与Action类名同样的资源文件。它只能在该Action中访问。如此一来,我们就可以在不同的Action里使用相同的properties名表示不同的值。例如,在ActonOne中title为“动作一”,而同样用title在ActionTwo表示“动作二”,节省一些命名工夫; 使用<s:i18n>标志访问特定路径的properties文件。使用方法请参考我早前的文章《常用的Struts 2.0的标志(Tag)介绍》。在您使用这一方法时,请注意<s:i18n>标志的范围。在<s:i18n name="xxxxx">到</s:i18n>之间,所有的国际化字符串都会在名为xxxxx资源文件查找,如果找不到,Struts 2.0就会输出默认值(国际化字符串的名字)。
====================用户自行选择语言Struts 2.0为您提供了一个名i18n的拦截器(Interceptor),并在默认情况下将其注册到拦截器链(Interceptor chain)中。它的原理为在执行Action方法前,i18n拦截器查找请求中的一个名为"request_locale"的参数。如果其存在,拦截器就将其作为参数实例化Locale对象,并将其设为用户默认的区域(Locale),最后,将此Locale对象保存在session的名为“WW_TRANS_I18N_LOCALE”的属性中。
---------package tutorial;
import java.util.Hashtable;import java.util.Locale;import java.util.Map;
publicclass Locales { public Map<String, Locale> getLocales() { Map<String, Locale> locales =new Hashtable<String, Locale>(2); locales.put("American English", Locale.US); locales.put("Simplified Chinese", Locale.CHINA); return locales; }}-------------<%@taglib prefix="s" uri="/struts-tags"%><script type="text/javascript"><!-- function langSelecter_onChanged() { document.langForm.submit(); }//--></script><s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/><s:bean id="locales" name="tutorial.Locales"/><form action="<s:url includeParams="get" encode="true"/>" name="langForm" style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;"> Language: <s:select label="Language" list="#locales.locales" listKey="value" listValue="key" value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE" name="request_locale" id="langSelecter" οnchange="langSelecter_onChanged()" theme="simple"/></form>-------------------<s:include value="/LangSelector.jsp"/>作为一个组件来使用。