本文主要例用简单的例子来讲解webwork对国际化的支持。
为了降低代码的编写量,减少视频文件的大小。本文主要针对上几个视频进行改写。在阅读本文之前请参lyx.iteye.com博客上的相关视频.
开发环境
Eclipse3.2
Myeclipse.5.1
Tomcat 5.5.9
PropertiesEditor插件
在struts中我们可以用 <html:messages key="””"> </html:messages> 来对页面进行国际化,我们也可以通过ActionMessages,ActionMessage两个类对Validator国际化。<o:p></o:p>
在struts中建立好对应的国际化文件.properties文件,例如。ApplicationResources_zh_CN.properties中文。ApplicationResources_en_US.properties英语。并对struts-config.xml文件进行配置<o:p></o:p>
<message-resources parameter="com.lyx.struts.ApplicationResources" />这样就可以完成国际化了。<o:p></o:p>
当然大家可以参考struts的源码研究一下实现的原理。<o:p></o:p>
我们首先来看一下页面国际化的相关内容。<o:p></o:p>
在 src 目录下新建 messages_zh_CN.properties 文件,内容如下:<o:p></o:p>
Label.username=用户名<o:p></o:p>
Label.password=密码<o:p></o:p>
Label.welcome=欢迎您{0}<o:p></o:p>
在刚才这一步大家注意看我的操作,刚才的过程实质是由PropertiesEditor插件完成.当然
你也可以把该文件编辑完,通过native2ascii命令来完成
格式如下:
Native2ascii message_zh_CN.properties 任意一个文件名
然后再将你转换后的文件的文件名改回来就可以了.
你也可以建立一个批处理文件也就是.bat文件.这样更方便些,随时可以执行.
在这里我们用这个插件,更加的方便.<o:p></o:p>
在 src 目录下新建 messages_en_US.properties 文件,内容如下:<o:p></o:p>
Label. username =username<o:p></o:p>
Label. password =password<o:p></o:p>
Label.welcome=welcome {0}<o:p></o:p>
修改index页面代码如下:<o:p></o:p>
<%@ page language="java" contentType="text/html; charset=UTF-8"%><o:p></o:p>
把字符集改成UTF-8的,为了支持国际化.<o:p></o:p>
<%@ taglib prefix="ww" uri="/webwork"%><o:p></o:p>
<html><o:p></o:p>
<head><o:p></o:p>
<title>My JSP 'index.jsp' starting page<!---->title><o:p></o:p>
<body><o:p></o:p>
<ww:i18n name="messages"><o:p></o:p>
<ww:form name="test" action="login.action" method="POST"><o:p></o:p>
<ww:textfield label="%{getText('label.username')}" name="userName" required="true" /><o:p></o:p>
<ww:password label="%{getText('label.password')}" name="userPwd" required="true" /><o:p></o:p>
<ww:submit value="Submit" /><o:p></o:p>
<!---->ww:form><o:p></o:p>
<!---->ww:i18n><o:p></o:p>
<!---->body><o:p></o:p>
<!---->html><o:p></o:p>
修改success页面代码如下:<o:p></o:p>
<ww:i18n name="messages"><o:p></o:p>
<ww:text name="label.welcome"><o:p></o:p>
<ww:param><ww:property value="user.userName" /><!---->ww:param><o:p></o:p>
<!---->ww:text><o:p></o:p>
<!---->ww:i18n><o:p></o:p>
基本代码就这些。测试一下看看吧。看看能不能得到我们想要的结果。<o:p></o:p>
OK了.<o:p></o:p>
第二部分:验证部份国际化。<o:p></o:p>
对于本文登陆的LoginAction要对应一个资源文件。命名以Action开头后面跟语言地区<o:p></o:p>
例如本文应LoginAction_zh_CN.properties。并放到同一目录下<o:p></o:p>
代码如下:<o:p></o:p>
error.username=用户名称必须输入!<o:p></o:p>
error.password=密码必须输入!<o:p></o:p>
同样再建一个LoginAction_en_US.properties代码如下:<o:p></o:p>
error.username=username is required!<o:p></o:p>
error.password=password is required!<o:p></o:p>
对验证部份进行修改<o:p></o:p>
LoginAction-validation.xml<o:p></o:p>
<!---->DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"><o:p></o:p>
<validators><o:p></o:p>
<field name="userName"><o:p></o:p>
<field-validator type="requiredstring"><o:p></o:p>
<message key="error.username">username is required! <!---->message><o:p></o:p>
<!---->field-validator><o:p></o:p>
<!---->field><o:p></o:p>
<field name="userPwd"><o:p></o:p>
<field-validator type="requiredstring"><o:p></o:p>
<message key="error.password">password is required! <!---->message><o:p></o:p>
<!---->field-validator><o:p></o:p>
<!---->field><o:p></o:p>
<!---->validators><o:p></o:p>
使用 key 来指定资源文件中的键值<o:p></o:p>
Ok了.全部通过.就到这里吧.<o:p></o:p>
我的联系方式:<o:p></o:p>
MSN:lyx_2709@hotmail.com<o:p></o:p>
Email:lyx_happy@163.com<o:p></o:p>
希望大家多提宝贵意见.<o:p></o:p>