您的位置:首页 > 编程语言 > Java开发

struts2充分国际化案例 错误解决

2015-09-22 08:41 381 查看
在struts2中须要做国际化的有:

jsp页面的国际化,action错误信息的国际化,转换错误信息的国际化。校验错误信息的国际化

在之前的样例中已经做过和国际化相关的样例了,在struts.xml中配置过

view
plaincopy
to clipboardprint?

<constant name="struts.custom.i18n.resources" value="message"></constant>

当中的message就是国际化资源文件的baseNmae。

我们先看看简单的在jsp中进行国际化

在src文件夹下新建message_en_US.properties。内容为

hello=add user

新建message_zh_CN.properties文件。内容为

hello=\u589e\u52a0\u7528\u6237

然后改动register2.jsp

要想使用国际化显示,能够将信息加入到<s:text></s:text> 标签中,也能够放在<s:i18n></s:i18n> 中,

在这里,先使用标签 <s:text></s:text>

添加下面内容:

view
plaincopy
to clipboardprint?

<s:text name="hello"></s:text>

重新启动server后,浏览该页,默认会显示出“添加用户”,能够在IE中打开Internet选项,在常规中选择语言,添加英语(美国)[en-US],然后设置为第一项,刷新就能够看到输出“add user”。

这边非常可能会出一种错误,例如以下:

The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when
the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]

at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)

at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)

at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)

at org.apache.jsp.basicinfo.basicInfo_jsp._jspx_meth_struts_005fform_005f0(basicInfo_jsp.java:122)

at org.apache.jsp.basicinfo.basicInfo_jsp._jspService(basicInfo_jsp.java:93)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

at java.lang.Thread.run(Thread.java:619)

web.xml相关配置例如以下:

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

<init-param>

<param-name>config</param-name>

<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml </param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

原因是由于过滤器仅仅过滤action结尾的请求

但我在JSP页面中使用了 struts的tag

所以请求不会被struts的过滤器处理

标签也就无法解析

改动web.xml就可以:

<url-pattern>/*</url-pattern>

action错误的国际化

在message_en_US.properties中添加下面内容

username.invalid=username invalid...

在message_zh_CN.properties中添加下面内容

username.invalid=\u7528\u6237\u540d\u4e0d\u5408\u6cd5...

改动RegisterAction中的validate方法,将错误加到ActionError中。在这里将使用到ActionSupport中的getText方法获得和国际化资源文件相关的信息。

以username验证为例:

view
plaincopy
to clipboardprint?

if (null == username || username.length() < 5 || username.length() > 10) {

this.addActionError(this.getText("username.invalid"));

}

这样就从资源文件里读取username.invalid的值。添加到ActionError中。

查看该页面不输入不论什么数据。提交后就能够看到显示效果了。

验证框架的国际化(field级别错误)

在message_en_US.properties文件里添加下面内容

username.xml.invalid=validate information

在message_zh_CN.properties文件里添加下面内容

username.xml.invalid=\u9a8c\u8bc1\u6846\u67b6\u4fe1\u606f

然后改动验证框架,须要将在properties文件里的内容添加到框架中。

以username为例

view
plaincopy
to clipboardprint?

<field name="username">

<field-validator type="requiredstring">

<param name="trim">true</param>

<messagekey="username.xml.invalid"></message>

</field-validator>

</field>

在message标签中添加属性key。值为properties文件里的key

标签中key大多是和国际化相关的

国际化资源文件的分类

当应用程序非常大时,须要国际化的东西会非常多,因此须要将国际化资源文件进行分类。

须要知道的是在src中的properties文件是全局资源文件,另外还能够分为包级别的和类级别的

首先看看包级别的

命名规则为package_language_country.properties

新建package_en_US.properties。内容为

username.xml.invalid=package validate information

新建package_zh_CN.properties,内容为

username.xml.invalid=\u5305\u9a8c\u8bc1\u4fe1\u606f

能够看到输出的信息为“包验证信息”,由此可见包级别的国际化资源文件的优先级高于全局国际化资源文件。

类级别

新建RegisterAction_en_US.properties,内容为

username.xml.invalid=class validate information

新建RegisterAction_zh_CN.properties。内容为

username.xml.invalid=\u7c7b\u9a8c\u8bc1\u4fe1\u606f

此时能够看到输出的信息为“类验证信息”。

由此能够得到国际化资源文件的优先级

全局<包级别<类级别

另外要进行表单的国际化时。要去掉theme="simple"

在RegisterAction_en_US.properties中添加

username.name=username

在RegisterAction_zh_CN.properties中添加

username.name=\u7528\u6237\u540d

改动表单标签

view
plaincopy
to clipboardprint?

<s:textfield name="username" key="username.name"></s:textfield>

注意到key通常是和国际化相关的。

另外除了用

另外除了用<s:text>这个标签外。还能够使用<s:i18n>这个标签

view
plaincopy
to clipboardprint?

<s:i18n name="temp"></s:i18n>

标签中包括name,代表着能够定义资源文件的baseName。如能够定义成temp。那么相应着

temp_en_US.properties和temp_zh_CN.properties这两个资源文件。

如定义:

view
plaincopy
to clipboardprint?

<s:i18n name="hello">

<s:text name="world">

<s:param>struts2</s:param>

</s:text>

</s:i18n>

注意到能够在<s:text>标签中添加<s:i18n> 标签。

在hello_en_US.properties文件里添加

world=hello {0}

hello_zh_CN.properties中添加

world=\u4f60\u597d,struts2

在struts2的默认拦截器栈中已经定义了i18n拦截器,所以struts2已经是一个国际化的框架了。

struts2会查找从client提交的request_locale属性,并存到session中的WW_TRANS_I18N_LOCALE字段

中。

这个<s:text> 标签外,还能够使用<s:i18n> 这个标签

view
plaincopy
to clipboardprint?

<s:i18n name="temp"></s:i18n>

总结一下显示方法:

<s:textname="hello"></s:text>

getText("username.invalid")

<messagekey="username.xml.invalid"></message>

<s:textfield name="username" key="username.name"></s:textfield>

<s:i18n name="temp"></s:i18n>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: