Struts2配置國際化資源,struts2國際化資源

來源:互聯網
上載者:User

Struts2配置國際化資源,struts2國際化資源

1. 國際化的目標

1). 如何配置國際化資源檔

I. Action 範圍資源檔: 在Action類檔案所在的路徑建立名為 ActionName_language_country.properties 的檔案


II. 包範圍資源檔: 在包的根路徑下建立檔案名稱為 package_language_country.properties 的屬性檔案,
一旦建立,處於該包下的所有 Action 都可以訪問該資源檔。注意:包範圍資源檔的 baseName 就是package,不是Action所在的包名。

 


III. 全域資源檔
> 命名方式: basename_language_country.properties
> struts.xml <constant name="struts.custom.i18n.resources" value="baseName"/>

第一步:在src目錄下添加兩個資源檔,命名方式很重要的。格式:baseName_language_country.properties。還有兩種命名方式,都可以的,這個不用過多研究。比如,實現中英文切換我們要添加 resource_zh_CN.properties 和 resource_en_US.properties。然後將各個key-value對添加到這兩個檔案中,key欄位一定要統一。如:

      

其中,value值可以使用預留位置,比如:你好{0},至於怎麼傳參數,稍後再解釋。

第二步:配置struts.properties檔案,這個檔案也是放在src目錄下的。

第一個索引值對是為了告訴程式,國際化資源檔是loginResource開頭命名的資源檔。

到這裡其實配置就算完事兒了,接下來就是在各個地方使用。

(1)在jsp中使用。舉兩個例子。

  <s:text name="login.title"></s:text>

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

  還可以輸出帶預留位置的資訊。如,有一個 login.welcome = 你好{0},那麼可以用下面這種方式來顯示:

  <s:text name="login.welcome"><s:param><s:property value="username"/></s:param></s:text>

(2)在Action中使用。舉個例子:

  getText("login.username");

  還可以使用帶預留位置的資訊。如,有一個 login.welcome = 你好{0},那麼可以用下面這種方式來顯示:

  String params[] = {"張三"};

  String welcome = getText("login.welcome", params);

第三步,實現在jsp頁面中的中英文切換

  index.jsp中主要部分代碼如下:

1       <a href="lang.action?request_locale=zh_CN">中文</a>2       <a href="lang.action?request_locale=en_US">English</a>3       4       <s:form id="loginform" action="login" method="post">5           <s:textfield name="username" key="login.username"></s:textfield>6           <s:textfield name="psd" key="login.psd" ></s:textfield>7           <s:submit key="login.submit"></s:submit>8       </s:form>

  主要看前兩行代碼,lang.action其實就是一個實現頁面轉換的Action,沒有什麼實質性的內容,隨便定義一個HelloAction.java,在execute()方法中返回個success就行了。url中的這個參數 request_locale會被 i18n 攔截器讀取,然後根據這個值設定語言環境。 i18n 攔截器是struts中default裡面內建的攔截器。

  struts.xml配置如下:

 1         <action name="login" class="com.main.action.LoginAction"> 2             <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping> 3             <result name="success">/success.jsp</result> 4             <result name="error">/index.jsp</result> 5             <result name="input">/index.jsp</result> 6             <result name="regist">/regist.jsp</result>     7         </action> 8          9         <action name="lang" class="com.main.action.HelloAction">10             <result name="success">/index.jsp</result>11         </action>

  哦了,基本完成了,接下來就是看效果啦。運行一下:

     

 

擴充知識:

圖解:

 

 

文字說明:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.