中文系统系统默认使用zh_CN文件,没有的话使用基本文件lan.properties
lan.properties
label.ahtitle=wuhan diff veryday label.username=user name ah001=i need input m01={0}is already registed e01=call admin please
lan_zh_CN.properties
label.ahtitle=\u5149\u7070\u57CE\u5E02 label.username=\u7528\u6237\u540D ah001=\u9700\u8981\u4F60\u7684\u8F93\u5165 m01={0}\u5DF2\u6CE8\u518C e01=\u627E\u7BA1\u7406\u5458
inputMsg.jsp 无关大局,略了
index.jsp(JSP页面国际化)
<title><s:text name="label.ahtitle" /></title> ... <% // 改一下国家,火狐浏览器可以改,360,Chrome没发现 ActionContext.getContext().setLocale(new Locale("","")); %> <s:form action="m1Action" method="post"> <s:textfield name="uname" key="label.username" /> <s:actionmessage/> <s:submit value="%{getText(‘label.ahtitle‘)}"></s:submit> <s:actionerror/> </s:form>
#Ognl中的%,类似js的eval功能,用于强制返回Ognl结果
title中的内容,测试没啥效果(火狐浏览器修改语言后有效果)
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.custom.i18n.resources" value="lan" /> <package name="p1" namespace="/" extends="struts-default"> <action name="*Action" class="org.ah.s2.C1" method="{1}"> <result name="success" type="dispatcher"> /inputMsg.jsp </result> <result name="input">/index.jsp</result> </action> </package> </struts>
Action:(编程式 验证错误消息国际化)
package org.ah.s2; import java.util.Collection; import com.opensymphony.xwork2.ActionSupport; public class C1 extends ActionSupport { private String uname; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String m1() { Collection<String> actionMessages = getActionMessages(); System.out.println("actionMessages:" + actionMessages); if (actionMessages.size() > 0) { return "input"; } return "success"; } @Override public void validate() { if ("admin".equals(uname)) { // 继续走(被效验)方法 addActionMessage(getText("m01", new String[] { uname })); } else if ("sa".equals(uname)) { // 直接input,不走(被效验)方法 addActionError(getText("e01")); } } }
C1-validation.xml(声明式 验证错误消息国际化)
<validators> <field name="uname"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="ah001"></message> </field-validator> </field> </validators>
(引入日文资源后,发现错误提示还是中文,用火狐改浏览器语言应该可解决之)
lan_ja_JP.properties
label.ahtitle=\u6B66\u6F22 label.username=\u540D\u524D ah001=\u5165\u529B\u3057\u3067\u304F\u3060\u3055\u3044 m01={0}\u3082\u3046 e01=\u7BA1\u7406\u54E1\u306B
时间: 2024-10-19 21:56:23