struts2--国际化
1. 创建资源文件,放在action包里:App_en_US.properties, App_zh_CN.properties.
在properties界面用add的方式添加变量,添加完成后, 打开source如下:
login.username=username\: login.password=password\: login.email=email login.submit=submit login.username.isnull=username is null login.password.isnull=password is null login.email.isnull=email is null login.password.lengtherror=password length must be 4~6 login.email.formaterror=email format is error login.field.isnull={0} is null label.username=username label.password=password label.email=email
login.username=\u7528\u6237\u540D\: login.password=\u5BC6\u7801\: login.email=\u7535\u5B50\u90AE\u4EF6 login.submit=\u786E\u5B9A login.username.isnull=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A login.password.isnull=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A login.email.isnull=\u7535\u5B50\u90AE\u7BB1\u4E0D\u80FD\u4E3A\u7A7A login.password.lengtherror=\u5BC6\u7801\u5FC5\u987B4~6\u4F4D login.email.formaterror=\u7535\u5B50\u90AE\u4EF6\u683C\u5F0F\u4E0D\u6B63\u786E login.field.isnull={0}\u4E0D\u80FD\u4E3A\u7A7A label.username=\u7528\u6237\u540D label.password=\u5BC6\u7801 label.email=\u7535\u5B50\u90AE\u7BB1
2. struts.xml加载属性文件, 配置在所有包最上面 struts里面, 添加下面这句话:
<constant name="struts.custom.i18n.resources" value="App"></constant>
3. login.jsp页面读取资源文件:
//选择语言
<a href="<%=path%>/language.action?request_locale=en_US">英文</a>|<a href="<%=path%>/language.action?request_locale=zh_CN">中文</a>
<s:property value="getText('label.username')"/>
<s:property value="getText('label.password')"/>
<s:property value="getText('label.email')"/>
<br><input type="button" value="<s:property value="getText('login.submit')"/>" onclick="javascript:document.loginForm.action='<%=path%>/user/login1.action';document.loginForm.submit();"/>
4. struts.xml里在default包里添加:
<action name="language"> <result>/login.jsp</result> </action>
5.validation.xml里加载属性信息:
validation.xml(UsersAction2-validationl.xml)里的出错信息也可以用国际化文件:
${getText("login.field.isnull",{getText("label.username")})} 传递参数, login.field.isnull里有{0}
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message> ${getText("login.field.isnull",{getText("label.username")})} </message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message> ${getText("login.field.isnull",{getText("label.password")})} </message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">6</param> <message key="login.password.lengtherror"></message> </field-validator> </field> <field name="email"> <field-validator type="requiredstring"> <message> ${getText("login.field.isnull",{getText("label.email")})} </message> </field-validator> <field-validator type="email"> <message key="login.email.formaterror"></message> </field-validator> </field> </validators>