Struts 2的国际化支持
[size=3][font=宋体]前面已经介绍了[/font][font=Times New Roman]Java[/font][font=宋体]国际化的原理和方法,读者知道,[/font][font=Times New Roman]Java[/font][font=宋体]的国际化需要一个[/font][font=Times New Roman]Locale[/font][font=宋体]和一个资源包就能够实现国际化。资源包可以是资源文件也可以是资源类文件。[/font][/size][size=3][font=Times New Roman]Struts 2[/font][font=宋体]的国际化是建立在[/font][font=Times New Roman]Java[/font][font=宋体]国际化的基础之上的,也是使用资源包的方式,通过[/font][font=Times New Roman]getBundle()[/font][font=宋体]方法来寻找指定[/font][font=Times New Roman]Locale[/font][font=宋体]相关联的资源包,再从资源包文件中查找指定[/font][font=Times New Roman]Key[/font][font=宋体]所对应的国际化资源信息。[/font][/size]
[size=3][font=Times New Roman]Struts 2[/font][font=宋体]框架的底层国际化与[/font][font=Times New Roman]Java[/font][font=宋体]国际化是一致的,作为一个良好的[/font][font=Times New Roman]MVC[/font][font=宋体]框架,[/font][font=Times New Roman]Struts 2[/font][font=宋体]将[/font][font=Times New Roman]Java[/font][font=宋体]的国际化功能进行了封装和简化,开发者使用起来会更加简单快捷。[/font][/size]
[size=3][font=宋体][size=3][font=Times New Roman]Struts 2[/font][font=宋体]强调的是各个组件之间的松散耦合,而各个组件之间都是通过配置文件来实现相互关联和交互的。[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的国际化也是如此。[/font][/size]
[size=3][font=Times New Roman]Struts 2[/font][font=宋体]框架提供了多种加载国际化资源文件的方式,其中最常用的就是读者前面已经熟悉的加载资源文件的方式来实现国际化。[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架加载资源文件一般都是通过常量设置来完成的。[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的默认配置文件[/font][font=Times New Roman]struts-deault.xml[/font][font=宋体]中已经定义了国际化拦截器,部分内容如下:[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义拦截器[/font][font=Courier New] -->[/font][/size]
[size=2][font=Courier New]<interceptors>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义拦截器[/font][font=Courier New]alias -->[/font][/size]
[size=2][font=Courier New]
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义拦截器[/font][font=Courier New]autowiring -->[/font][/size]
[size=2][font=Courier New]<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />[/font][/size]
[size=2][font=Courier New]<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />[/font][/size]
[size=2][font=Courier New]<interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义异常拦截器[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义文件上传拦截器[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义国际化拦截器[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>[/font][/size]
[size=2][font=Courier New]<!—[/font][font=宋体]定义日志拦截器[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>[/font][/size]
[size=2][font=Courier New]<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>[/font][/size]
[size=2][font=Courier New]… …[/font][/size]
[size=3][font=宋体]可见,该拦截器为[/font][font=Times New Roman]i18n[/font][font=宋体],对应的[/font][font=Times New Roman]class[/font][font=宋体]为[/font][font=Times New Roman]com.opensymphony.xwork2.interceptor.I18n Interceptor[/font][font=宋体],有兴趣的读者可以参考其[/font][font=Times New Roman]API[/font][font=宋体]文档。[/font][/size]
[size=3][font=Times New Roman]I18nInterceptor[/font][font=宋体]是一个拦截器类,该拦截器在[/font][font=Times New Roman]Action[/font][font=宋体]执行处理之前执行,该拦截器掌管着当前用户请求[/font][font=Times New Roman]Session[/font][font=宋体]中的[/font][font=Times New Roman]Locale[/font][font=宋体]相关数据。该拦截器会在用户参数中查找一个特殊的参数值,使用该参数来设置当然的[/font][font=Times New Roman]Locale[/font][font=宋体]信息,这就意味着开发者可以动态地修改当前[/font][font=Times New Roman]session[/font][font=宋体]中的[/font][font=Times New Roman]Locale[/font][font=宋体]值,这在开发国际化应用中非常有意义,开发者可以在处理用户请求过程中任何一个节点来改变[/font][font=Times New Roman]Locale[/font][font=宋体]值,这样就能够动态改变程序的语言和区域的相关信息,实现完善的国际化功能。[/font][/size]
[size=3][font=宋体]例如,使用默认的拦截器参数,假设一个用户请求为[/font][font=Times New Roman]foo.action?request_locale=en_US[/font][font=宋体],那么英语(语言)和美国(国家)将会被保存到请求的[/font][font=Times New Roman]session[/font][font=宋体]中,在以后的请求处理过程中会被使用。该拦截器有下面两个参数:[/font][/size]
[size=3][/size]
[size=3][font=Times New Roman]parameterName[/font][font=宋体]:可选参数,即用户请求参数中包含[/font][font=Times New Roman]Locale[/font][font=宋体]值的参数名称,默认为[/font][font=Times New Roman]request_locale[/font][font=宋体]。[/font][/size]
[size=3][/size]
[size=3][font=Times New Roman]attributeName[/font][font=宋体]:可选参数,[/font][font=Times New Roman]session[/font][font=宋体]中被选用的[/font][font=Times New Roman]Locale[/font][font=宋体]值的[/font][font=Times New Roman]key[/font][font=宋体],默认为[/font][font=Times New Roman]WW_TRANS_I18N_LOCALE[/font][font=宋体]。[/font][/size]
[size=3][font=宋体]配置资源文件常量,即配置[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的[/font][font=Times New Roman]struts.custom.i18n.resources[/font][font=宋体]常量,该常量定义了[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架全局国际化资源文件的[/font][font=Times New Roman]basename[/font][font=宋体]。[/font][/size]
[size=3][font=宋体]如果开发者需要在项目应用中提供国际化功能,则需要指定[/font][font=Times New Roman]struts.custom.i18n.resources[/font][font=宋体]常量值。配置[/font][font=Times New Roman]struts.custom.i18n.resources[/font][font=宋体]常量,可以在属性文件[/font][font=Times New Roman]struts.properties[/font][font=宋体]中定义,也可以在配置文件[/font][font=Times New Roman]struts.xml[/font][font=宋体]或者[/font][font=Times New Roman]web.xml[/font][font=宋体]文件中定义。[/font][/size]
[size=3][font=宋体]假设要定义配置一个[/font][font=Times New Roman]basename[/font][font=宋体]为[/font][font=Times New Roman]globalMessages[/font][font=宋体]的[/font][font=Times New Roman]struts.custom.i18n.resources[/font][font=宋体]常量,可以在[/font][font=Times New Roman]struts.properties[/font][font=宋体]文件中做如下配置:[/font][/size]
[size=2][font=Courier New]#[/font][font=宋体]在属性文件中定义[/font][font=Courier New]basename[/font][/size]
[size=2][font=Courier New]struts.custom.i18n.resources=globalMessages[/font][/size]
[size=3][font=宋体]也可以在[/font][font=Times New Roman]struts.xml[/font][font=宋体]文件中配置[/font][font=Times New Roman]basename[/font][font=宋体]:[/font][/size]
[size=2][font=Courier New]I18N[/font][font=宋体]资源文件为[/font][font=Courier New]globalMessages[/font][/size]
[size=2][font=Courier New]
<constant name="struts.custom.i18n.resources"
value="globalMessages" />[/font][/size]
[size=3][font=宋体]同样,也可以在[/font][font=Times New Roman]web.xml[/font][font=宋体]文件中定义:[/font][/size]
[size=2][font=Courier New]… …[/font][/size]
[size=2][font=Courier New]<!--[/font][font=宋体]定义[/font][font=Courier New]struts.custom.i18n.resources[/font][font=宋体]常量[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<init-param>[/font][/size]
[size=2][font=Courier New]
<param-name>struts.custom.i18n.resources</param-name>[/font][/size]
[size=2][font=Courier New]
<param-value>globalMessages</param-value>[/font][/size]
[size=2][font=Courier New]
</init-param>[/font][/size]
[size=2][font=Courier New]… …[/font][/size]
[size=3][font=宋体]配置好[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的国际化资源文件的[/font][font=Times New Roman]basename[/font][font=宋体]后,开发者就可以按照[/font][font=Times New Roman]basename_language_country.properties[/font][font=宋体]的命名规则来建立不同语言环境的资源文件了,当然,如果是非西欧字符集,则需要使用[/font][font=Times New Roman]native2ascii[/font][font=宋体]转换工具转换为[/font][font=Times New Roman]Unicode[/font][font=宋体]编码即可。[/font][/size]
[/font][/size] [font=宋体]一旦开发者指定了[/font][font=Times New Roman]struts.custom.i18n.resources[/font][font=宋体]常量,即指定了国际化资源文件的[/font][font=Times New Roman]basename[/font][font=宋体],那么就可以开发国际化应用了。下面以一个注册的示例来演示[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的国际化功能。值得注意的是,该示例与前面章节的用户注册是有所差别的。注册用户[/font][font=Times New Roman]Action[/font][font=宋体]关系图如图[/font][font=Times New Roman]4.6[/font][font=宋体]所示。[/font]
[img]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/4-6.jpg[/img]
[size=2][font=宋体]图[/font][font=Times New Roman]4.6
[/font][font=宋体]注册应用[/font][font=Times New Roman]Action[/font][font=宋体]关系图[/font][/size]
[font=Times New Roman](1) [/font][font=宋体]建立中文和英文的资源文件,[/font][font=Times New Roman]globalMessages_en_US.properties[/font][font=宋体]内容如代码[/font][font=Times New Roman]4.7[/font][font=宋体]所示。[/font]
[font=黑体]代码[/font][font=Arial]4.7
[/font][font=黑体]英文资源文件[/font][font=Arial]globalMessages_en_US.properties[/font]
[size=2][font=Courier New]#[/font][font=宋体]英文资源文件内容[/font][/size]
[size=2][font=Courier New]HelloWorld=Hello World![/font][/size]
[size=2][font=Courier New]user=username[/font][/size]
[size=2][font=Courier New]pass=password[/font][/size]
[size=2][font=Courier New]username=Your Name[/font][/size]
[size=2][font=Courier New]password1=Password[/font][/size]
[size=2][font=Courier New]password2=confirm Password[/font][/size]
[size=2][font=Courier New]birthday=Birthday[/font][/size]
[size=2][font=Courier New]regpage=Reg Page[/font][/size]
[size=2][font=Courier New]errpage=ERROR Page[/font][/size]
[size=2][font=Courier New]successlogin=Welcom[/font][/size]
[size=2][font=Courier New]falselogin=Sorry!You can't log in[/font][/size]
[size=2][font=Courier New]regsuccess=OK,You reg success![/font][/size]
[size=2][font=Courier New]regfalse=Sorry! You Reg False![/font][/size]
[size=2][font=Courier New]regbutton=Reg![/font][/size]
[size=3][font=Times New Roman][size=5pt]
[/size](2)globalMessages_zh_CN.properties[/font][font=宋体]内容如代码[/font][font=Times New Roman]4.8[/font][font=宋体]所示。[/font][/size]
[font=黑体]代码[/font][font=Arial]4.8
[/font][font=黑体]中文资源文件[/font][font=Arial]globalMessages_zh_CN.properties[/font]
[size=2][font=Courier New]#[/font][font=宋体]简体中文资源文件内容[/font][/size]
[size=2][font=Courier New]HelloWorld=[/font][font=宋体]你好,世界![/font][/size]
[size=2][font=Courier New]name=[/font][font=宋体]用户名称[/font][/size]
[size=2][font=Courier New]pass=[/font][font=宋体]用户密码[/font][/size]
[size=2][font=Courier New]username=[/font][font=宋体]注册用户名[/font][/size]
[size=2][font=Courier New]password1=[/font][font=宋体]密码[/font][/size]
[size=2][font=Courier New]password2=[/font][font=宋体]确认密码[/font][/size]
[size=2][font=Courier New]birthday=[/font][font=宋体]生日[/font][/size]
[size=2][font=Courier New]regpage=[/font][font=宋体]注册界面[/font][/size]
[size=2][font=Courier New]errpage=[/font][font=宋体]错误界面[/font][/size]
[size=2][font=Courier New]successlogin=[/font][font=宋体]登录成功[/font][/size]
[size=2][font=Courier New]falselogin=[/font][font=宋体]登录失败[/font][/size]
[size=2][font=Courier New]regsuccess=[/font][font=宋体]注册成功[/font][/size]
[size=2][font=Courier New]regfalse=[/font][font=宋体]对不起,注册失败![/font][/size]
[size=2][font=Courier New]regbutton=[/font][font=宋体]注册[/font][/size]
[size=3][font=Times New Roman][size=5pt]
[/size](3)globalMessages_zh_CN.properties[/font][font=宋体]文件为中文资源文件,该文件在使用前,必须使用[/font][font=Times New Roman]native2ascii[/font][font=宋体]转换工具转换。接下来建立输入界面[/font][font=Times New Roman]reg.jsp[/font][font=宋体],如代码[/font][font=Times New Roman]4.9[/font][font=宋体]所示。[/font][/size]
[font=黑体]代码[/font][font=Arial]4.9
[/font][font=黑体]输入界面[/font][font=Arial]reg.jsp[/font]
[size=2][font=Courier New]<%@ page contentType="text/html;charset=UTF-8" language="java" %>[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="s" uri="/struts-tags" %>[/font][/size]
[size=2][font=Courier New]<html>[/font][/size]
[size=2][font=Courier New]<head>[/font][/size]
[size=2][font=Courier New]<title><s:text name="regpage"/></title>[/font][/size]
[size=2][font=Courier New]<s:head />[/font][/size]
[size=2][font=Courier New]</head>[/font][/size]
[size=2][font=Courier New]<body>[/font][/size]
[size=2][font=Courier New]<table>[/font][/size]
[size=2][font=Courier New]<s:form id="id" action="Reg">[/font][/size]
[size=2][font=Courier New]
<!—[/font][font=宋体]使用[/font][font=Courier New]key[/font][font=宋体]来加载国际化资源信息[/font][font=Courier New] -->[/font][/size]
[size=2][font=Courier New]
<s:textfield name="username" key="username"/>[/font][/size]
[size=2][font=Courier New]
<s:password name="password1" key="password1"/>[/font][/size]
[size=2][font=Courier New]
<s:password name="password2" key="password2"/>[/font][/size]
[size=2][font=Courier New]
<s:datetimepicker name="birthday" key="birthday" displayFormat="yyyy-MM-dd"/>[/font][/size]
[size=2][font=Courier New]
<s:submit key="regbutton"/>[/font][/size]
[size=2][font=Courier New]</s:form>[/font][/size]
[size=2][font=Courier New]</table>[/font][/size]
[size=2][font=Courier New]</body>[/font][/size]
[size=2][font=Courier New]</html>[size=10.5pt][/size][/font][/size]
[font=Times New Roman]reg.jsp[/font][font=宋体]中使用了标签库来访问资源文件,[/font][font=Times New Roman]<s:text/>[/font][font=宋体]是显示静态文本,该标签中可以使用[/font][font=Times New Roman]key[/font][font=宋体]属性来输出国际化信息。[/font][font=Times New Roman]Form[/font][font=宋体]元素的标签也可以使用[/font][font=Times New Roman]key[/font][font=宋体]来获得国际化信息。有关标签库的知识,后面将会详细讲解,在这里读者只需要简单了解。[/font]
[font=Times New Roman](4) [/font][font=宋体]同样,[/font][font=Times New Roman]success.jsp[/font][font=宋体]也使用了标签库,如代码[/font][font=Times New Roman]4.10[/font][font=宋体]所示。[/font]
[font=黑体]代码[/font][font=Arial]4.10
[/font][font=黑体]注册成功界面[/font][font=Arial]success.jsp[/font]
[size=2][font=Courier New]<%@ page contentType="text/html;charset=UTF-8" language="java" %>[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="s" uri="/struts-tags" %>[/font][/size]
[size=2][font=Courier New]<html>[/font][/size]
[size=2][font=Courier New]<head>[/font][/size]
[size=2][font=Courier New]<title><s:text name="regsuccess"/></title>[/font][/size]
[size=2][font=Courier New]<s:head />[/font][/size]
[size=2][font=Courier New]</head>[/font][/size]
[size=2][font=Courier New]<body>[/font][/size]
[size=2][font=Courier New]<table>[/font][/size]
[size=2][font=Courier New]<h2><s:text name="username"/>[/font][font=宋体]:[/font][font=Courier New]<s:property
value="username" /></h2>[/font][/size]
[size=2][font=Courier New]<h2><s:text name="password1"/>[/font][font=宋体]:[/font][font=Courier New]<s:property
value="password1" /></h2>[/font][/size]
[size=2][font=Courier New]<h2><s:text name="birthday"/>[/font][font=宋体]:[/font][font=Courier New]<s:property
value="birthday" /></h2>[/font][/size]
[size=2][font=Courier New]</table>[/font][/size]
[size=2][font=Courier New]</body>[/font][/size]
[size=2][font=Courier New]</html>[size=10.5pt][/size][/font][/size]
[font=宋体]上面两个[/font][font=Times New Roman]JSP[/font][font=宋体]用户视图,所有的显示内容都使用了国际化信息,可以根据用户不同的语言与区域配置,来显示相应的国际化内容。[/font]
[font=宋体]那么,用户视图可以访问国际化资源,在[/font][font=Times New Roman]Action[/font][font=宋体]中可以访问吗?答案是可以的,前面已经介绍过了,[/font][font=Times New Roman]Struts 2[/font][font=宋体]提供了一个[/font][font=Times New Roman]ActionSupport[/font][font=宋体]工具类,开发自己的[/font][font=Times New Roman]Action[/font][font=宋体],只需要继承该类就可以。在该类的[/font][font=Times New Roman]API[/font][font=宋体]文档中,可以发现,该类提供了一个[/font][color=windowtext][font=Times New Roman]getText[/font][/color][font=Times New Roman]([/font][color=windowtext][font=Times New Roman]String[/font][/color][font=Times New Roman] aTextName)[/font][font=宋体]方法,该方法根据资源文件中的[/font][font=Times New Roman]key[/font][font=宋体]值来返回一个国际化资源信息,如果找不到则为[/font][font=Times New Roman]null[/font][font=宋体]。[/font]
[font=Times New Roman](5) [/font][font=宋体]在本示例的[/font][font=Times New Roman]Action[/font][font=宋体]中,会调用[/font][color=windowtext][font=Times New Roman]getText[/font][/color][font=Times New Roman]([/font][color=windowtext][font=Times New Roman]String[/font][/color][font=Times New Roman] aTextName)[/font][font=宋体]方法,获得资源文件中的国际化信息,并在控制台中打印出来,如代码[/font][font=Times New Roman]4.11[/font][font=宋体]所示。[/font]
[font=黑体]代码[/font][font=Arial]4.11
[/font][font=黑体]国际化业务控制器[/font][font=Arial]reg[/font]
[size=2][font=Courier New]package ch4;[/font][/size]
[size=2][font=Courier New]import java.util.Date;[/font][/size]
[size=2][font=Courier New]import com.opensymphony.xwork2.ActionSupport;[/font][/size]
[size=2][font=Courier New]public class Reg extends ActionSupport {[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义用户名属性[/font][/size]
[size=2][font=Courier New]
private String username;[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义处理信息:注意与[/font][font=Courier New]http[/font][font=宋体]中的[/font][font=Courier New]msg[/font][font=宋体]名称不同[/font][/size]
[size=2][font=Courier New]
private String mymsg;[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义密码属性[/font][/size]
[size=2][font=Courier New]
private String password1;[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义确认密码[/font][/size]
[size=2][font=Courier New]
private String password2;[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义生日属性[/font][/size]
[size=2][font=Courier New]
private Date birthday;[/font][/size]
[size=2][font=Courier New]
public String execute() throws Exception {[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]判断用户输入参数[/font][/size]
[size=2][font=Courier New]
if (username != null && getPassword1().equals(getPassword2())[/font][/size]
[size=2][font=Courier New]
&& !getUsername().trim().equals("")) {[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]打印国际化信息[/font][/size]
[size=2][font=Courier New]
System.out.println(getText("username") + ":" + username);[/font][/size]
[size=2][font=Courier New]
System.out.println(getText("password1") + ":" + password1);[/font][/size]
[size=2][font=Courier New]
System.out.println(getText("birthday") + ":" + birthday);[/font][/size]
[size=2][font=Courier New]
return SUCCESS;[/font][/size]
[size=2][font=Courier New]
} else {[/font][/size]
[size=2][font=Courier New]
return INPUT;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
//getter[/font][font=宋体]和[/font][font=Courier New]setter[/font][font=宋体]方法[/font][/size]
[size=2][font=Courier New]
public String getUsername() {[/font][/size]
[size=2][font=Courier New]
return username;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public void setUsername(String username) {[/font][/size]
[size=2][font=Courier New]
this.username = username;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public String getMymsg() {[/font][/size]
[size=2][font=Courier New]
return mymsg;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public void setMymsg(String mymsg) {[/font][/size]
[size=2][font=Courier New]
this.mymsg = mymsg;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public String getPassword1() {[/font][/size]
[size=2][font=Courier New]
return password1;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public void setPassword1(String password1) {[/font][/size]
[size=2][font=Courier New]
this.password1 = password1;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public String getPassword2() {[/font][/size]
[size=2][font=Courier New]
return password2;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public void setPassword2(String password2) {[/font][/size]
[size=2][font=Courier New]
this.password2 = password2;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public Date getBirthday() {[/font][/size]
[size=2][font=Courier New]
return birthday;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]
public void setBirthday(Date birthday) {[/font][/size]
[size=2][font=Courier New]
this.birthday = birthday;[/font][/size]
[size=2][font=Courier New]
}[/font][/size]
[size=2][font=Courier New]}[/font][/size]
[font=Times New Roman] [/font][font=宋体](6)运行该应用,在中文、英文的语言与区域配置环境中,注册界面分别如图[/font][font=Times New Roman]4.7[/font][font=宋体]和图[/font][font=Times New Roman]4.8[/font][font=宋体]所示。当设置语言与区域选项为“简体中文”时,[/font][font=Times New Roman]Tomcat[/font][font=宋体]控制台会打印出中文信息:[/font]
[size=2][font=宋体]注册用户名[/font][font=Courier New]:pla[/font][/size]
[size=2][font=宋体]密码[/font][font=Courier New]:123456[/font][/size]
[size=2][font=宋体]生日[/font][font=Courier New]:Wed Nov 28 00:00:00 CST 2007[/font][/size]
[font=宋体]当设置语言与区域选项为“英语美国”时,[/font][font=Times New Roman]Tomcat[/font][font=宋体]控制台会打印出英文信息:[/font]
[size=2][font=宋体]信息[/font][font=Courier New]: Detected AnnotationActionValidatorManager, initializing it...[/font][/size]
[size=2][font=Courier New]Your Name:pla[/font][/size]
[size=2][font=Courier New]Password:123456[/font][/size]
[size=2][font=Courier New]Birthday:Sun Oct 28 00:00:00 CST 2007[/font][/size]
[size=2][font=Courier New][/font][/size]
[font=Times New Roman] [img=315,262]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/4-7.jpg[/img][/font]
[size=2][font=宋体]图[/font][font=Times New Roman]4.7
[/font][font=宋体]中文注册界面[/font][font=Times New Roman] [/font][/size]
[size=2][font=Times New Roman] [img]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/4-8.jpg[/img] [/font][/size]
[size=2][font=Times New Roman][/font][/size]
[size=2][font=Times New Roman] [/font][font=宋体]图[/font][font=Times New Roman]4.8
[/font][font=宋体]英文注册界面[/font][/size]
[font=宋体]分别在中文和英文配置下,输入“[/font][font=Times New Roman]pla[/font][font=宋体]”等注册信息,单击“注册”按钮或者“[/font][font=Times New Roman]Reg![/font][font=宋体]”按钮,结果分别如图[/font][font=Times New Roman]4.9[/font][font=宋体]和图[/font][font=Times New Roman]4.10[/font][font=宋体]所示,界面显示的所有元素都使用国际化信息输出。[/font]
[img]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/4-9.jpg[/img]
[font=Times New Roman]
[/font]
[size=2][font=Times New Roman] [/font][font=宋体]图[/font][font=Times New Roman]4.9
[/font][font=宋体]注册成功中文界面[/font][font=Times New Roman] [/font][/size]
[size=2][img]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/4-10.jpg[/img][/size]
[size=2]
[font=宋体]图[/font][font=Times New Roman]4.10
[/font][font=宋体]注册成功英文界面[/font][/size]
[font=Times New Roman]Action[/font][font=宋体]中可以使用[/font][font=Times New Roman]ActionSupport[/font][font=宋体]类提供的[/font][font=Times New Roman]getText(String aTextName)[/font][font=宋体]方法来获得国际化信息,证实了前面所讲的国际化拦截器将用户请求参数中的[/font][font=Times New Roman]Locale[/font][font=宋体]值存入了当前[/font][font=Times New Roman]session[/font][font=宋体]中,[/font][font=Times New Roman]Action[/font][font=宋体]正是获得了与[/font][font=Times New Roman]Locale[/font][font=宋体]相关联的国际化信息资源。[/font]
页:
[1]