Struts2 2.0 国际化(i18n): 国际化是商业中不可或缺的一部分(提供不同语言,在不同国家都可以使用): 无论学习的是什么Web框架,它都是必须掌握的技能: • Struts1.0 极大的简化了我们程序员在国际化时所需要的工作。例如:我们输出一条国际化的信息。只要在代码中加入file-name-xx-XX.properties(其中File-name为默认资源文件的文件名),然后在struts-config.xml中指定其路径。再在页面用<bean:message>标签输出即可。 • Struts2.0 的使用方式: 1. 根据 Locale 信息 2. 创建资源文件 3. 页面输出 例子1 (Java Project): package Dome; import java.util.Locale; import java.util.ResourceBundle; public class T { public static void main(String[] args) { Locale currenLoacld=new Locale("en","CA"); Resource Bundle messages=ResourceBundle.getBundle("T",currenLoacld); System.out.println(messages.getString("Lin.xiaoling")); } } • 添加T.properties文件:Lin.xialling 为该文件的key • 可以通过JDK 形式支持,一般情况下我们都用MyEclipse7.0中的plugin 插件支持。操作步骤为: a). 下载MyEclipse7.0 plugins: (com.essiembre.eclipse.i18n.resourcebundle_0.7.7) b).存放路径为: (C:/Program Files/MyEclipse7.0M1/eclipse/plugins) c).重新启动MyEclipse7.0 ;右键使用Open_With中的资源文件编辑器打开T.properties配置文件编辑即可。 例子2 (Web Project): Struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--取得配置文件中的value --> 资源文件 <constant name="struts.custom.i18n.resources" value="T" /> <!--使程序处于开发状态--> <constant name="struts.devMode" value="true" /> <package name="international_test" namespace="/international_test" extends="struts-default"> <action name="HelloWorld" class="international_test.HelloWorld" > <result>/final.jsp</result> </action> </package> </struts> <constant name="struts.custom.i18n.resources" value="T" /> 页面输出: <%@ page contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Hello World</title> </head> <body> <h2><s:text name="Lin.xiaoling "/></h2> <h2><s:property value="%{getText('Lin.xiaoling ')}"/></h2> </body> </html> 两种输出方式:Lin.xiaoling:对应T.properties配置文件的Key ************************************************************************ 上面的例子用了两种方法来显示国际化字符串,其输出是相同的。其实,这就是Struts 2.0的一个优势,因为它默认支持EL,所示我们可以用getText方法来简洁地取得国际化字符串。另外更普遍的情况——在使用UI表单标志时,getText可以用来设置label属性,例如: <s:textfield name="name" label="%{getText('UserName')}"/> 资源文件查找顺序: 之所以说Struts2.0的国际化更灵活是因为它可以根据不同需要配置和获取资源(properties)文件, 在Struts2.0中有下面集中方式: 1. 使用全局的资源文件,方法如上例所示。 这适用与遍布于整个应用程序的国际化字符串,他们在不同的包(package)中被引用,如一些比较公用的出错提示; 2. 使用包范围内的资源文件。做法是在包的根目录ixa新建名为package.properites和包package_xx_XX.properties 问价。这就设用于在包中不同类访问的资源 3. 使用Action范围的资源文件。做法为Action的包下新建文件名(除扩展名外)与Action类同样的资源文件。它只能在该Actino中访问。如此一来,我们就可以在不同的Action里使用相同的properties名为不同的值。例如,在ActonOne中title为“动作一”,而同样用title在ActionTwo表示“动作二”,节省一些命名工夫; 4. 使用<s:i18n>标志访问特定路径的properties文件。使用方法请参考我早前的文章《常用的Struts 2.0的标志(Tag)介绍》。在您使用这一方法时,请注意<s:i18n>标志的范围。在<s:i18n name="xxxxx">到</s:i18n>之间,所有的国际化字符串都会在名为xxxxx资源文件查找,如果找不到,Struts 2.0就会输出默认值(国际化字符串的名字)。 参数化国际化字符串: 许多情况下,我们都需要在运行时(runtime)为国际化字符串插入一些参数,例如在输入验证提示信息的时候,在struts2.0中,我们通过一下两种方法做到这点: 1. 在资源文件的国际化字符串中使用OGNL,格式为${表达式},例如: validation.require=${getText(fileName)} is required 2. 使用java.text.MessageFormat中的字符串格式,格式为{ 参数序号(从0开始), 格式类形(number | date | time | choice), 格式样式},例如: validation.between=Date must between {0, date, short} and {1, date, short} 在显示这些国际化字符时,同样有两种方法设置参数的值: 1. 使用标志的value0、value1...valueN的属性,如: <s:text name="validation.required" value0="User Name"/> 2. 使用param子元素,这些param将按先后顺序,代入到国际化字符串的参数中,例如: <s:text name="validation.required"> <s:param value="User Name"/> </s:text> 例子: package demo; import java.util.*; import java.text.*; public class MessageFormatDemo { static void displayMessage(Locale currentLocale) { System.out.println("currentLocale = " + currentLocale.toString()); ResourceBundle messages = ResourceBundle.getBundle("MessageBundle", currentLocale); Object[] messageArguments = { new Date(), new Integer(88), new String("玲珑")}; MessageFormat formatter = new MessageFormat(""); // formatter.setLocale(currentLocale); formatter.applyPattern(messages.getString("template")); String output = formatter.format(messageArguments); System.out.println("===========:"+output); } static public void main(String[] args) { displayMessage(new Locale("de", "DE")); } } 该文件通过多个参数获取 Action 中取资源文件的value :this.getText(“key”);