struts.custom.i18n.resources

本文介绍了Struts2框架中的国际化实现方法,包括页面、Action及校验信息的国际化配置,并详细解释了不同级别的资源文件及其优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化

 

首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>

 

资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties

 

1. jsp页面的国际化 
通过使用标签<s:text name="label.helloWorld"/>输出国际化
label.helloWorld为资源文件中定义的key

 


在messageResource_en_US.properties加入以下内容
label.hello=hello {0}
label.helloWorld=hello,world

在messageResource_zh_CN.properties加入以下内容
label.hello=你好 {0}
label.helloWorld=你好,世界

 

(1). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面两个都为输出一个hello word的两种表示

 

<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
显示一个文本框,文本框的标题进行国际化

 

(2). 使用<s:i18n>标签指定从某个特定的资源文件中取数据
<s:i18n name="messageResource">
   <s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在从messageResource取资源

 

(3).
<s:text name="label.hello">
   <s:param>callan</s:param>
</s:text>
使用带参数的资源.<s:param>可以替换label.hello=hello {0}中的{0}

 

2. Action的国际化
Action的国际化主要是通过getText(String key)方法实现的

 

 

  1. public String execute() throws Exception {   
  2.   
  3.            
  4.   
  5.         // getText(String) string为key   
  6.   
  7.          String str1 = getText("label.helloWorld");   
  8.   
  9.          System.out.println(str1);   
  10.   
  11.            
  12.   
  13.         // 带参数的   
  14.   
  15.          String str2 = getText("label.hello",new String[]{"fjf"});   
  16.   
  17.          System.out.println(str2);   
  18.   
  19.        
  20.   
  21.         // 与上一种实现一样   
  22.   
  23.          List l = new ArrayList();   
  24.   
  25.          l.add("callan");   
  26.   
  27.          String str3 = getText("label.hello",l);   
  28.   
  29.          System.out.println(str3);   
  30.   
  31.            
  32.   
  33.         return SUCCESS;   
  34.   
  35.      }  

 

3. 参数化国际化
在messageResource_en_US.properties加入以下内容
userName=userName
userName.required=${getText('userName')} is required

 

在messageResource_zh_CN.properties加入以下内容
userName=用户名
userName.required=${getText('userName')} 不能为空

 

在Action中
String str4 = getText("userName.required");
System.out.println(str4);

 

userName.required=${getText('userName')}会取国际化的用户名

 

4. 使用校验框价时,提示信息可以国际化
   <field name="userName">
   <field-validator type="requiredstring">
    <message key=”userName.required”> </message>
   </field-validator>
</field>

 


国际化资源文件分为三种级别
(1) 全局资源文件,可以被整个应该程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包级资源文件,每个包的根目录下可以新建资源文件,仅被当前包中的类访问.文件名格式为:package_语言代码_国家代码.
(3) Action级资源文件,仅被当前Action引用,名称为action名_语言代码_国家代码
查找顺序为从小范围到大范围, Action级优先级最大

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   <init-param>
            <param-name>struts.custom.i18n.resources</param-name>
            <param-value>globalMessages</param-value>
        </init-param>
</filter>

<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

<?xml version="1.0" encoding="UTF-8" ?> <!-- /* * $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <bean class="com.inc.sbu.view.FreeMarkerPageFilter" static="true" optional="true"/> <constant name="struts.ognl.allowStaticMethodAccess" value="true" /> <constant name="struts.convention.relative.result.types" value="freemarker,dispatcher"/> <constant name="struts.convention.action.mapAllMatches" value="true"/> <constant name="struts.convention.package.locators" value="web,action" /> <constant name="struts.convention.default.parent.package" value="yk-default"/> <constant name="struts.convention.result.path" value="/pages"/> <constant name="struts.convention.result.flatLayout" value="true"/> <constant name="struts.i18n.encoding" value="gbk" /> <constant name="struts.i18n.reload" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.serve.static.browserCache" value="false"/> <constant name="struts.configuration.xml.reload" value="false" /> <constant name="struts.custom.i18n.resources" value="globalMessages" /> <constant name="struts.freemarker.manager.classname" value="customFreemarkerManager" /> <constant name="struts.multipart.saveDir" value="/tmp"/> <constant name="struts.multipart.maxSize" value="1000000000" /> <constant name="struts.ui.theme" value="simple"/> <constant name="struts.action.extension" value="xhtml,,json" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="yk-default" extends="struts-default"> <result-types> <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult"/> <result-type name="json" class="org.apache.struts2.json.JSONResult"/> </result-types> <interceptors> <interceptor class="com.inc.sbu.struts.AuctionInterceptor" name="auctionInterceptor"/> <interceptor class="com.inc.sbu.struts.TokenInterceptor" name="incTokenInterceptor"/> <interceptor class="com.inc.sbu.struts.WorkFlowInterceptor" name="incFlowInterceptor"/> <interceptor class="com.inc.sbu.util.FileUploadInterceptor" name="fileUploadInterceptor"/> <interceptor class="com.inc.sbu.struts.ClewInterceptor" name="clewInterceptor"/> <interceptor-stack name="bidStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="profiling"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="auctionInterceptor"/> </interceptor-stack> <interceptor-stack name="subStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="fileUploadInterceptor"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,inputxy,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="clewInterceptor"> <param name="excludeMethods">login,loginManager,logout,index</param> </interceptor-ref> <interceptor-ref name="incTokenInterceptor"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="subStack"/> <global-results> <result name="invalid.token">/pages/error/repeat-submit.jsp</result> <result name="clew" type="dispatcher">/pages/clew.jsp</result> <result name="manage-clew" type="dispatcher">/pages/manage-clew.jsp</result> <result name="auction-clew" type="dispatcher">/pages/auction-clew.jsp</result> <result name="syserror" type="dispatcher">/pages/error.jsp</result> <result name="validerror" type="dispatcher">/pages/error.jsp</result> <result name="frontpurviewerror" type="dispatcher">/pages/error/frontpurviewerror.jsp</result> <result name="frontloginerror" type="dispatcher">/pages/error/frontpurviewerror.jsp</result> <result name="fronterror" type="dispatcher">/pages/error/frontticketerror.jsp</result> <result name="filenotfound" type="dispatcher">/pages/error/filenotfound.jsp</result> <result name="login" type="redirect">/login</result> <result name="adminLogin" type="dispatcher">/login.jsp</result> <result name="login" type="redirect">/login</result> <result name="entran" type="redirect">/entran</result> <!-- <result name="adminLogin" type="dispatcher">/login.jsp</result> --> <result name="auctionAdminError" type="redirect">/auction/adminLogin</result> <result name="auctionActorError" type="redirect">/auction/login</result> </global-results> </package> <!-- Overwrite Convention --> </struts> 我现在需要重写fileUpload,即不再使用fileUpload的拦截器,需要怎么配置
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值