首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Struts 國際化程式設計輕鬆實作:完整步驟與資源檔管理

     編輯:Java 相關知識

前言

Struts 是 MVC 框架,像 Java 與其他 Java 框架一樣,可輕鬆實作國際化 (i18n)。筆者根據網路資料嘗試,因首次開發多語言程式走過不少彎路,但經不斷嘗試終於成功實作簡單多語言頁面。全程手工編碼,未使用輔助工具。

實作步驟

1. 建立伺服器

使用 Tomcat 4 作為測試環境,建立過程略。

2. 下載 Struts

至 官網 下載,解壓後將 .war 檔案複製到 Tomcat webapps 目錄,啟動 Tomcat。若 https://localhost:8080/struts-example/ 正常執行,環境建立成功。.war 檔啟動後會自動展開成檔案,內含原始碼可供研究。

3. 建立專案目錄結構

在 webapps 下建立 international 資料夾,再建立 WEB-INF 與 WEB-INF/classes,這是 JSP 專案必備結構。

4. 加入 Struts 類別庫

在 WEB-INF 下建立 lib 子目錄,將 struts-example\WEB-INF\lib 所有 .jar 複製過去:

  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar
  • commons-fileupload.jar
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar

5. 加入 Struts 標籤定義檔

從 struts-example\WEB-INF 複製 .tld 檔案到 international\WEB-INF:

  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-nested.tld
  • struts-template.tld
  • struts-tiles.tld

6. 建立 struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
  "https://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  <message-resources parameter="resources.application"/>
</struts-config>

<message-resources> 指定訊息資源檔案,resources.application 代表 classes/resources 目錄下資源檔,預設語言檔 application.properties,中文 application_zh_CN.properties,其他語言類推。

7. 建立 web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "https://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
  <display-name>test international</display-name>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <taglib>
    <taglib-uri>/tags/struts-bean</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/tags/struts-html</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/tags/struts-logic</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/tags/struts-nested</taglib-uri>
    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/tags/struts-tiles</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
</web-app>

定義 Struts 控制類別、config 檔案與標籤庫,因簡單不再解釋。

8. 建立資源檔

在 classes 下建立 resources 目錄存放資源檔。先建立預設 application.properties 與英文 application_en_US.properties:

# -- international test --
test.title=international application test
test.body=This is a international application test

中文資源檔下一步建立。

9. 建立 index.jsp

在 international 目錄下建立 index.jsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html:html locale="true">
<head>
  <title><bean:message key="test.title"/></title>
  <html:base/>
</head>
<body bgcolor="white">
  <p><bean:message key="test.body"/></p>
</body>
</html:html>

<html:html locale="true"> 表示使用瀏覽器預設地區與語言;<bean:message key="test.title"/> 取對應資源檔 test.title 內容。啟動 Tomcat,瀏覽器輸入 https://localhost:8080/international/,若標題顯示 international application test、頁面顯示 This is a international application test 即成功。後續只需增加資源檔即可支援多語言。

10. 建立簡體中文資源檔

在 resources 下建立 application_cn.properties:

# -- international test --
test.title=國際化程式測試
test.body=這是一個國際化程式測試例子

Java 國際化透過 Unicode 實現,需轉換為 Unicode。在 DOS 下進入 resources 目錄執行:

native2ascii application_cn.properties application_zh_CN.properties

轉換後 application_zh_CN.properties 內容:

# -- international test --
test.title=\u56fd\u9645\u5316\u7a0b\u5e8f\u6d4b\u8bd5
test.body=\u8fd9\u662f\u4e00\u4e2a\u56fd\u9645\u5316\u7a0b\u5e8f\u6d4b\u8bd5\u4f8b\u5b50

重新啟動 Tomcat,瀏覽器輸入網址,標題與內容應變為中文。

11. 建立繁體中文資源檔

建立 application_tw.properties:

# -- international test --
test.title=國際化程式測試
test.body=這是一個國際化程式測試例子

執行:

native2ascii application_tw.properties application_zh_TW.properties

轉換後:

# -- international test --
test.title=\u570b\u969b\u5316\u7a0b\u5f0f\u6e2c\u8a66
test.body=\u9019\u662f\u4e00\u500b\u570b\u969b\u5316\u7a0b\u5f0f\u6e2c\u8a66\u4f8b\u5b50

12. 測試多語言

開啟 IE「工具」→「網際網路選項」→「一般」標籤→「語言」按鈕,新增「英語 (美國) [en-us]」,刪除其他語言,重啟 IE 後輸入網址,內容變英文。同法測試簡體、繁體中文。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved