国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

struts國(guó)際化程序嘗試

2019-11-18 15:56:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  struts是一個(gè)MVC框架,像java和其他Java框架一樣,struts可以輕松實(shí)現(xiàn)國(guó)際化;于是根據(jù)網(wǎng)上的資料,做了一個(gè)嘗試,因?yàn)榈谝淮巫龆嗾Z(yǔ)言程序,還是拐了很多彎路;但所幸,經(jīng)過(guò)不斷的嘗試,終于成功的實(shí)現(xiàn)多語(yǔ)言版本的簡(jiǎn)單頁(yè)面; 因?yàn)槌绦蚍浅:?jiǎn)單,所以在整個(gè)嘗試過(guò)程中,全部使用手工編碼,沒(méi)有使用任何輔助工具;
  
  1、 建立服務(wù)器
  我使用Tomcat4作為測(cè)試環(huán)境,建立過(guò)程(略);
  
  2、 下載struts
  可以到http://jakarta.apache.org/struts/index.Html下載,下載后解壓,把其中的.war文件拷貝到Tomcat的webapps目錄下,啟動(dòng)Tomcat,假如http://localhost:8080/struts-example/ 運(yùn)行沒(méi)有問(wèn)題,說(shuō)明環(huán)境建立成功;這些.war文件在Tomcat啟動(dòng)后會(huì)自動(dòng)展開成文件,里面有源代碼,可以作為源碼研究;
  
  3、 建立工程
  在webapps目錄下建立一個(gè)international文件夾,再在international目錄下建立WEB-INF文件夾和WEB-INF/classes文件夾,這些都是一個(gè)jsp工程必須的;
  
  4、 加了struts的類
  在WEB-INF目錄下建立一個(gè)lib子目錄,把struts-example/WEB-INF/lib目錄下將所有.jar文件拷貝到該目錄下;這些文件是struts的控制類庫(kù)和標(biāo)簽類庫(kù)等;
  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標(biāo)簽定義文件
  從struts-example/WEB-INF目錄下,把.TLD文件拷貝到international的WEB-INF目錄下,這些文件標(biāo)簽庫(kù)的定義文件;
  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,內(nèi)容如下:
  
  <?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標(biāo)簽是指message資源的文件,就是我們存放我們的多種語(yǔ)言的提示信息的文件,resources.application表是classes目錄下的resources目錄用來(lái)存放資源文件,默認(rèn)語(yǔ)言文件名為application.PRoperties,中文為application_zh_CN.properties,其他語(yǔ)言類似;
  
  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 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>
  上面的web.xml定義了struts的控制類、config文件和標(biāo)簽,因?yàn)楸容^簡(jiǎn)單,所以不做解釋;
  
  8、 建立資源文件
  在classes目錄下,建立一個(gè)resources目錄,用來(lái)存放資源文件; 先建立默認(rèn)的資源文件application.properties和英文(美國(guó))的資源文件application_en_US.properties,內(nèi)容為:
  
  # -- international test --
  test.title=international application test
  test.body=This is a international application test
  
  先建立這兩個(gè)文件,中文的等下一步建立
  
  9、建立jsp文件
  在international目錄下,建立index.jsp文件,內(nèi)容為:
  
  <%@ 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">表示使用瀏覽器默認(rèn)的地區(qū)和語(yǔ)言;<bean:message key="test.title"/>的意思是取對(duì)應(yīng)資源文件里的test.title項(xiàng)目的內(nèi)容; 啟動(dòng)Tomcat,在瀏覽器里輸入http://localhost:8080/international/,查看效果,假如瀏覽器標(biāo)題顯示international application test,頁(yè)面里顯示This is a international application test則說(shuō)明你的程序成功了;下面只要增加資源文件,你就可以在多種語(yǔ)言的系統(tǒng)里看了;
  
  10、 建立簡(jiǎn)體中文的資源文件
  在resources目錄下建立一個(gè)application_cn.properties,輸入內(nèi)容:
  
  # -- international test --
  test.title=國(guó)際化程序測(cè)試
  test.body=這是一個(gè)國(guó)際化程序測(cè)試?yán)?
  
  因?yàn)閖ava的國(guó)際化是通過(guò)unicode碼來(lái)實(shí)現(xiàn),所以要把代碼轉(zhuǎn)為unicode碼;在Dos下,轉(zhuǎn)到resources目錄,執(zhí)行:
  native2ascii application_cn.properties application_zh_CN.properties
  轉(zhuǎn)換后的application_zh_CN.properties文件內(nèi)容為:
  
  # -- 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碼; 重新啟動(dòng)Tomcat, 在瀏覽器里輸入http://localhost:8080/international/,你看,標(biāo)題和內(nèi)容是不是變成中文了;
  
  11、 建立繁體中文的資源文件
  在resources目錄下建立一個(gè)application_tw.properties,輸入內(nèi)容:
  
  # -- international test --
  test.title=???H化程式?y??
  test.body=?@是一?????H化程式?y??例子
  
  因?yàn)閖ava的國(guó)際化是通過(guò)unicode碼來(lái)實(shí)現(xiàn),所以要把代碼轉(zhuǎn)為unicode碼;在Dos下,轉(zhuǎn)到resources目錄,執(zhí)行:
  native2ascii application_tw.properties application_zh_TW.properties
  轉(zhuǎn)換后的application_zh_TW.properties文件內(nèi)容為:
  
  # -- 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
  
  這就是上面的繁體中文的unicode碼;
  
  12、 測(cè)試多語(yǔ)言
  打開IE的“工具”->“Internet選項(xiàng)”菜單,“常規(guī)”選項(xiàng)卡,點(diǎn)擊其中的“語(yǔ)言”按鈕,添加“英語(yǔ)(美國(guó))-[en-us]”語(yǔ)

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临泉县| 双峰县| 库尔勒市| 会同县| 雅安市| 延吉市| 昌吉市| 塔城市| 黎川县| 长垣县| 博客| 齐河县| 洪洞县| 宜丰县| 榆中县| 开鲁县| 信丰县| 奉化市| 武平县| 泰来县| 新安县| 南木林县| 离岛区| 尖扎县| 富宁县| 菏泽市| 准格尔旗| 肇东市| 扶绥县| 普洱| 东宁县| 株洲市| 长垣县| 蒙阴县| 祁门县| 诸城市| 柘城县| 珲春市| 留坝县| 嘉峪关市| 淮滨县|