Struts 實現的I18N

來源:互聯網
上載者:User

struts 和I18n:
就是 International 後面18個字元。。具體意義省略。
其實現原理是:
服務端維護多套對應的資源檔,對應於不同的語言(這些資源檔獨立於代碼存在)。
訪問發送到伺服器時,伺服器自動識別用戶端所使用的語言環境。
伺服器根據客戶的語言環境選擇合適的資源,渲染入視圖,然後返回給用戶端。

struts 實現I18n
struts的struts-config 裡可以定義一個
<message-resources> element, 該標誌能夠導致struts引用到不同的資源檔。

實現的方式很簡單:
MessageResources_zh_CN
message.hello=hello..(cn)

sturts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans>
    </form-beans>

    <global-exceptions>
    </global-exceptions>
  
  <action-mappings>
   <action path="/view"
     type="com.bss.test.i18n.ActionI18N">
     <forward name="success" path="/ui/view.jsp" redirect="false" />
   </action>
  
    </action-mappings>

    <message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />
</struts-config>

 

view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<b>This page is for test I18n.<b><br>
Message:&nbsp;
<bean:message key="message.hello" bundle="lala"/>
</body>
</html>

這裡,這個<message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />
和<bean:message key="message.hello" bundle="lala"/>中的lala想對應,也就是說,可以有多個不同的MessageSources.
只要他們<message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />中的key不一樣就可以了。
可以自由的安排設定檔的部署。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.