【Struts2国际化资源文件定义的3种范围方法】

 1)全局的国际化资源文件,对所有的Action和View都有效

    定义方式:

        在struts.xml中增加全局资源文件定路径定义:
        <constant name="struts.custom.i18n.resources" value="globalMessage"></constant>

        对应的资源文件为classpath根目录位置:

        globalMessage.properties,

        globalMessage_en.properties,

        globalMessage_cn_ZH.properties

  2)包范围的国际化资源文件,对package目录下的Action和View有效

       不需要额外的配置,只需要将资源文件定义为 :

         package.properties,

        package_en.properties,

        package_cn_ZH.properties,

       并放在对应classpath所在的package目录下      

  3)Action范围的国际化资源文件,只对某个Action以及Action对应的View有效,资源文件已具体的action类名命名,

       如:RegisterAction.class 对应的资源文件为

        RegisterAction.properties, 

        RegisterAction_en.properties,

        RegisterAction_cn_ZH.properties;

       资源文件与Action类放在同一目录位置,不需要额外配置即可生效;

【Struts2国际化资源的引用方式】

 1)在View(JSP)文件中的引用方式

    使用Struts2标签

  •         <s:text name="label.helloWorld"/>输出国际化label.helloWorld为资源文件中定义的key;

  •        <s:property value="%{getText('label.helloWorld')}"/>

  •        <s:textfield name="name" key="label.helloWorld"/>
            <s:textfield name="name" label="%{getText('label.helloWorld')}"/>

  •       带参数方式

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

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

 2)在Action类中的引用方式

     

        Action的国际化主要是通过getText的三种方法实现的

    public String getText(String aTextName)

    public String getText(String aTextName,List args)

       public String getText(String aTextName,String defaultValue)

   

     如:String str2 = getText("label.hello",new String[]{"kypulo"});