posts - 297,  comments - 1618,  trackbacks - 0
                                                                                          

          文:阿蜜果
          日期:2008-1-2——1-3

          1. web.xml

                 web.xml文件對(duì)任何的Web項(xiàng)目都是一個(gè)必須的文件,使用Struts時(shí),還需要對(duì)該文件進(jìn)行一些必須的配置。

          1.1 ActionServlet的配置

          一般需要在該文件中配置StrutsServlet,示例配置如下:

          Eg1. 簡(jiǎn)單的StrutsActionServlet的配置:

          <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>3</param-value>

              </init-param>

              <init-param>

                <param-name>detail</param-name>

                <param-value>3</param-value>

              </init-param>

              <load-on-startup>0</load-on-startup>

           </servlet>

           <servlet-mapping>

              <servlet-name>action</servlet-name>

              <url-pattern>*.do</url-pattern>

           </servlet-mapping>

                 對(duì)于復(fù)雜的應(yīng)用,一般需要配置多個(gè)struts-config.xml文件,可以通過(guò)添加另外的<init-param>來(lái)實(shí)現(xiàn),或者在多個(gè)配置文件中以為“,”隔開(kāi),如下所示:

          Eg2. 配置多個(gè)struts-config.xml配置文件的ActionServlet的配置:

          <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>config/IVR</param-name>

                                      <param-value>/WEB-INF/struts-config-IVR.xml</param-value>

                             </init-param>

                             <init-param>

                             <param-name>config/wap</param-name>

                                      <param-value>

                                               /WEB-INF/struts-config-wap.xml

                                      </param-value>

                             </init-param>

                             <init-param>

                                      <param-name>debug</param-name>

                                      <param-value>3</param-value>

                             </init-param>

                             <init-param>

                                      <param-name>detail</param-name>

                                      <param-value>3</param-value>

                             </init-param>

                             <load-on-startup>0</load-on-startup>

                   </servlet>

          <servlet-mapping>

                         <servlet-name>action</servlet-name>

                         <url-pattern>*.do</url-pattern>

                </servlet-mapping>

          1.2 歡迎和錯(cuò)誤處理的配置

                 首先講述一下歡迎文件清單<welcome-file-list>的配置,該元素可包含多個(gè)<welcome-file>子元素,當(dāng)Web容器調(diào)用歡迎界面時(shí),將首先查看第一個(gè)<welcome-file>子元素中定義的文件是否存在,若存在,則將其返回給用戶(hù),若不存在,繼續(xù)判斷第二個(gè)<welcome-file>子元素中定義的文件……,配置示例如下:

          <welcome-file-list>

                             <welcome-file>index.html</welcome-file>

                             <welcome-file>index.jsp</welcome-file>

                             <welcome-file>default.jsp</welcome-file>

                   </welcome-file-list>

                 接著講述一下在web.xml中如何配置錯(cuò)誤處理,這時(shí)需要使用<error-page>元素,該元素可以根據(jù)異常的類(lèi)型來(lái)配置跳轉(zhuǎn)的頁(yè)面,還可以根據(jù)錯(cuò)誤碼來(lái)配置跳轉(zhuǎn)頁(yè)面,配置示例如下:

          <!-- 根據(jù)錯(cuò)誤碼進(jìn)行跳轉(zhuǎn)-->

          <error-page>

          <error-code>500</error-code>

          <location>/error.jsp</location>

          </error-page>

          <!-- 根據(jù)異常進(jìn)行跳轉(zhuǎn)-->

          <error-page>

          <exception-type>java.lang.NullException</exception-type>

          <location>/error.jsp</location>

          </error-page>

          1.3 tld文件的配置

                 Web工程沒(méi)有使用Struts的標(biāo)簽庫(kù),可以不在web.xml中使用Struts的標(biāo)簽庫(kù)信息。當(dāng)然若開(kāi)發(fā)人員使用了struts的標(biāo)簽庫(kù),也可以直接在jsp頁(yè)面中引入標(biāo)簽庫(kù),例如通過(guò)如下方式引入:

          <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>

          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

          <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

          <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>

                 Struts中進(jìn)行配置的的好處是因?yàn)榭梢栽?/span>Struts中配置為tld文件配置一個(gè)簡(jiǎn)要的名稱(chēng)或者更加易懂的名稱(chēng),例如在web.xml文件中增加如下配置:

          <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-uri>元素指定標(biāo)簽庫(kù)的相對(duì)或者絕對(duì)URI地址,Web應(yīng)用將根據(jù)這一URI來(lái)訪(fǎng)問(wèn)標(biāo)簽庫(kù);<taglib-location>元素指定標(biāo)簽庫(kù)描述文件在文件資源系統(tǒng)中的物理位置。

                 此時(shí)在jsp頁(yè)面通過(guò)如下方面引入標(biāo)簽庫(kù):

          <%@ taglib uri="/tags/struts-bean " prefix="bean"%>

          <%@ taglib uri="/tags/struts-html" prefix="html"%>

          <%@ taglib uri="/tags/struts-logic " prefix="logic"%>

          <%@ taglib uri="/tags/struts-nested " prefix="nested"%>

          1.4 完整配置實(shí)例

                 下面舉一個(gè)使用StrutsWeb項(xiàng)目的web.xml的簡(jiǎn)單配置實(shí)例(該實(shí)例開(kāi)發(fā)人員也參考struts-1.2.8-bin.zip包的webapps目錄下的struts-mailreader.war),內(nèi)容如下所示:

          <?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>Struts Example Application</display-name>

           <!-- Action Servlet Configuration -->

           <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, /WEB-INF/struts-config-registration.xml</param-value>

              </init-param>

              <load-on-startup>1</load-on-startup>

           </servlet>

           <!-- Action Servlet Mapping -->

           <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>

           <!-- 錯(cuò)誤處理 -->

           <error-page>

             <exception-type>java.lang.Exception</exception-type>

             <location>/error.jsp</location>

           </error-page>

           <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>

          </web-app>

          2 .properties資源文件

                 默認(rèn)情況下,Struts默認(rèn)的資源文件為ApplicationResources.properties文件。

          資源文件可為一個(gè)束,可針對(duì)不同的語(yǔ)言,寫(xiě)不同的資源文件,開(kāi)發(fā)人員可以定義英文、簡(jiǎn)體中文、繁體中文、日文等資源文件,資源文件的命名格式為:資源文件名_國(guó)別_語(yǔ)言.properties,常見(jiàn)的資源文件名稱(chēng)為:

                 ApplicationResources.properties:默認(rèn)資源文件,或英文資源文件

                 ApplicationResources_zh_CN.properties:簡(jiǎn)體中文

                 ApplicationResources_zh_TW.properties:繁體中文

                 每一個(gè)資源文件都是“鍵-值”對(duì)的集合,例如可在src目錄下的資源文件ApplicationResources.properties中,編寫(xiě)如下內(nèi)容:

          UserForm.userName = USERNAME

          UserForm.password = PASSWORD

                 同時(shí)在src目錄下建立ApplicationResources_zh_CN.properties.bak文件來(lái)編寫(xiě)簡(jiǎn)體中文的原始資源文件,內(nèi)容如下:

          UserForm.userName = 用戶(hù)名

          UserForm.password = 密碼

                 開(kāi)發(fā)人員還可以建立ApplicationResources_zh_TW.properties.bak文件編寫(xiě)繁體中文的原始資源文件。其內(nèi)容如下:

          UserForm.userName = ノめ?W

          UserForm.password = ノめ?W

                 對(duì)于簡(jiǎn)體中文和繁體中文文件,開(kāi)發(fā)人員還需要對(duì)其使用native2ascii工具對(duì)其進(jìn)行轉(zhuǎn)換,將非ASCII碼轉(zhuǎn)換為Unicode編碼,native2ascii工具在%JAVA_HOME%/bin目錄下的native2ascii.exe,因此若要進(jìn)行這種轉(zhuǎn)換,讀者首先需要安裝了該工具。安裝了該工具之后,進(jìn)入其所在目錄,運(yùn)行native2ascii…命令可進(jìn)行編碼轉(zhuǎn)換,為了能在命令行直接使用該命令,讀者還需要在系統(tǒng)變量PATH中添加路徑:%JAVA_HOME%"bin

                 在實(shí)際項(xiàng)目中,一般編寫(xiě)一個(gè)批處理文件(eg. code.bat)來(lái)處理資源文件的編碼,例如code.bat為如下內(nèi)容時(shí),可滿(mǎn)足要求將如上的ApplicationResources_zh_CN.properties.bak文件進(jìn)行編碼,并將編碼后的信息放入ApplicationResources_zh_CN.properties文件中。該批處理文件的內(nèi)容如下所示:

          del ApplicationResources_zh_CN.properties

          copy ApplicationResources_zh_CN.properties.bak ApplicationResources_zh_CN.properties.gbk

          native2ascii -encoding GBK ApplicationResources_zh_CN.properties.gbk ApplicationResources_zh_CN.properties

          del ApplicationResources_zh_CN.properties.gbk

          del ApplicationResources_zh_TW.properties

          copy ApplicationResources_zh_TW.properties.bak ApplicationResources_zh_TW.properties.big5

          native2ascii -encoding Big5 ApplicationResources_zh_TW.properties.big5 ApplicationResources_zh_TW.properties

          del ApplicationResources_zh_TW.properties.big5

          del *.bak.bak

                 運(yùn)行code.bat之后,可看到目錄下多出了兩個(gè)資源文件,即:ApplicationResources_zh_CN.propertiesApplicationResources_zh_TW.properties。其中ApplicationResources_zh_CN.properties的內(nèi)容是經(jīng)過(guò)編碼的,內(nèi)容如下:

          UserForm.userName = "u7528"u6237"u540d

          UserForm.password = "u5bc6"u7801

                 jsp頁(yè)面中,可以通過(guò)Struts的自定義標(biāo)簽來(lái)獲取資源文件的某個(gè)鍵(例如:UserForm.userName)的信息。示例如下:

          <bean:message key="UserForm.userName"/>

          參考文檔:
             

          《為Struts應(yīng)用配置web.xml文件》

          Struts國(guó)際化處理》

           

          posted on 2008-01-05 12:23 阿蜜果 閱讀(9689) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Struts


          FeedBack:
          # re: 【Struts1.2總結(jié)系列】web.xml、.properties資源文件的配置
          2008-01-05 12:30 | xfan
          用struts2吧,struts1簡(jiǎn)直在浪費(fèi)生命  回復(fù)  更多評(píng)論
            
          # re: 【Struts1.2總結(jié)系列】web.xml、.properties資源文件的配置
          2008-01-05 15:20 | wǒ愛(ài)伱--咾婆
          不是說(shuō)配置xml文件不要使用“-”為好嗎??郁悶。和我看到的不同。。喜歡JAVA風(fēng)格  回復(fù)  更多評(píng)論
            
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

                生活將我們磨圓,是為了讓我們滾得更遠(yuǎn)——“圓”來(lái)如此。
                我的作品:
                玩轉(zhuǎn)Axure RP  (2015年12月出版)
                

                Power Designer系統(tǒng)分析與建模實(shí)戰(zhàn)  (2015年7月出版)
                
               Struts2+Hibernate3+Spring2   (2010年5月出版)
               

          留言簿(263)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          相冊(cè)

          關(guān)注blog

          積分與排名

          • 積分 - 2299173
          • 排名 - 3

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 阿拉善盟| 阿荣旗| 崇明县| 汉川市| 类乌齐县| 辽阳县| 山阳县| 大厂| 托里县| 华阴市| 那坡县| 司法| 寿宁县| 延津县| 大邑县| 武山县| 新龙县| 赤水市| 西畴县| 南昌县| 延庆县| 宜章县| 集贤县| 旬阳县| 长武县| 南昌县| 外汇| 亚东县| 获嘉县| 会宁县| 荆州市| 肥乡县| 五常市| 林州市| 隆昌县| 泸水县| 太白县| 阿图什市| 界首市| 日照市| 潜江市|