zoukankan      html  css  js  c++  java
  • Struts2国际化

     一:简单理解

        国际化简称i18n,其来源是英文单词 internationalization的首末字符i 和n。18为中间的字符数。

        随着全球经济的一体化,软件开发者应该开发出支持多国语言、国际化的Web应用。对于Web应用来说,同样的页面在不同的语言环境下需要显示不同的效果。

        国际化文件的命名规则:

              1、基本名.properties 如:message.properties

              2、基本名_语言编码_国家编码.properties 如:message_zh_CN.properties, message_en_US.properties 其中语言编码和国家编码是固定的,可以在JDK中Locale类的常量中找到。

        Java中已经实现了国际化功能,struts2中只是对该功能进行了整合,以方便我们的使用。

        Struts2中使用到国际化的地方有: 1、jsp页面的国际化; 2、Action信息国际化; 3、转换错误信息的国际化; 4、校验错误信息的国际化;

        Struts2国际化文件分类: 1、全局范围国际化文件 2、包范围国际化文件 3、Action类范围国际化文件 。

      

        全局范围国际化文件:

        编写一个messages_zh_CN.properties和messages_en_US.properties放在src下。

    我在这里配置了两个属性:

    messages_en_US:

    login.username=username
    login.password=password

    messages_zh_CN:

    login.username=u767Bu5F55u7528u6237u540D
    login.password=u767Bu5F55u5BC6u7801

    在struts.xml中通过struts.custom.i18n.resources常量把资源文件定义为全局资源文件:

    eg:

                <!-- 配置struts2国际化 value是国际化资源基本名message-->
                <constant name="struts.custom.i18n.resources" value="messages_en_US"/>

          或者

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

    (java代码中)国际化获取配置文件值使用:getText("键")

            eg:getText("login.username")  ---------》username。

                  或者

               getText("login.username") ------------>登录用户名。

    在jsp页面中使用国际化。这里需要使用标签:<s:i18n>标签

    eg:

       

     1  <!-- 局部定义使用哪一种国际化语音 -->
     2    <s:i18n name="messages_zh_CN">  
     3    <form action="<%=basePath%>login.action" method="post">
     4              <table>
     5                <tr>
     6                  <td><s:text name="login.username"/></td>
     7                  <td><input type="text" name="user.userName"/></td>
     8                </tr>
     9                <tr>
    10                  <td><s:text name="login.password"/></td>
    11                  <td><input type="text" name="user.password"/></td>
    12                </tr>
    13                <tr>
    14                 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
    15               </tr>              
    16              </table>                             
    17     </form>
    18    </s:i18n> 

     浏览器显示为:

     1  <!-- 局部定义使用哪一种国际化语音 -->
     2    <s:i18n name="messages_en_US">  
     3    <form action="<%=basePath%>login.action" method="post">
     4              <table>
     5                <tr>
     6                  <td><s:text name="login.username"/></td>
     7                  <td><input type="text" name="user.userName"/></td>
     8                </tr>
     9                <tr>
    10                  <td><s:text name="login.password"/></td>
    11                  <td><input type="text" name="user.password"/></td>
    12                </tr>
    13                <tr>
    14                 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
    15               </tr>              
    16              </table>                             
    17     </form> 
    18    </s:i18n> 

    浏览器显示为

    新技术在不断的更新,该文章仅供参考!(最近好像在准备淘汰Struts)

  • 相关阅读:
    linux 短信收发
    sama5d3 环境检测 adc测试
    【Codeforces 723C】Polycarp at the Radio 贪心
    【Codeforces 723B】Text Document Analysis 模拟
    【USACO 2.2】Preface Numbering (找规律)
    【Codeforces 722C】Destroying Array (数据结构、set)
    【USACO 2.1】Hamming Codes
    【USACO 2.1】Healthy Holsteins
    【USACO 2.1】Sorting A Three-Valued Sequence
    【USACO 2.1】Ordered Fractions
  • 原文地址:https://www.cnblogs.com/hellokitty1/p/5083663.html
Copyright © 2011-2022 走看看