?? struts國際化輕松嘗試.txt
字號:
struts國際化輕松嘗試
1、 建立服務器
我使用Tomcat5.5作為測試環境,建立過程(略);
2、 下載struts
可以到http://jakarta.apache.org/struts/index.html下載.
3、 建立工程
在webapps目錄下建立一個international文件夾,再在international目錄下建立WEB-INF文件夾和WEB-INF/classes文件夾,這些都是一個JSP工程必須的;
4、 加了struts的類
在WEB-INF目錄下建立一個lib子目錄,把struts-example\WEB-INF\lib目錄下將所有.jar文件拷貝到該目錄下;這些文件是struts的控制類庫和標簽類庫等;
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文件
建立struts的config文件的struts-config.xml,內容如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<message-resources parameter="resources.application"/>
</struts-config>
message-resources標簽是指message資源的文件,就是我們存放我們的多種語言的提示信息的文件,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"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>test</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>
8、 建立資源文件
在classes目錄下,建立一個resources目錄,用來存放資源文件;
先建立默認的資源文件application.properties和英文(美國)的資源文件application_en_US.properties,內容為:
# -- test --
test.title=international application test
test.body=This is a international application test
先建立這兩個文件,中文的等下一步建立
9、建立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,在瀏覽器里輸入http://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 -encoding gb2312 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
這就是上面的中文的unicode碼;
重新啟動Tomcat, 在瀏覽器里輸入http://localhost:8080/international/,你看,標題和內容是不是變成中文了;
11、 測試多語言
打開IE的“工具”->“Internet選項”菜單,“常規”選項卡,點擊其中的“語言”按鈕,添加“英語(美國)-[en-us]”語言,將其他的語言刪除,重新啟動IE后,輸入http://localhost:8080/international/index.jsp,你會發現內容已經變成英文.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -