posts - 297,  comments - 1618,  trackbacks - 0
                                                                                          

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

          1. web.xml

                 web.xml文件對任何的Web項目都是一個必須的文件,使用Struts時,還需要對該文件進行一些必須的配置。

          1.1 ActionServlet的配置

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

          Eg1. 簡單的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>

                 對于復雜的應用,一般需要配置多個struts-config.xml文件,可以通過添加另外的<init-param>來實現,或者在多個配置文件中以為“,”隔開,如下所示:

          Eg2. 配置多個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 歡迎和錯誤處理的配置

                 首先講述一下歡迎文件清單<welcome-file-list>的配置,該元素可包含多個<welcome-file>子元素,當Web容器調用歡迎界面時,將首先查看第一個<welcome-file>子元素中定義的文件是否存在,若存在,則將其返回給用戶,若不存在,繼續判斷第二個<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中如何配置錯誤處理,這時需要使用<error-page>元素,該元素可以根據異常的類型來配置跳轉的頁面,還可以根據錯誤碼來配置跳轉頁面,配置示例如下:

          <!-- 根據錯誤碼進行跳轉-->

          <error-page>

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

          <location>/error.jsp</location>

          </error-page>

          <!-- 根據異常進行跳轉-->

          <error-page>

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

          <location>/error.jsp</location>

          </error-page>

          1.3 tld文件的配置

                 Web工程沒有使用Struts的標簽庫,可以不在web.xml中使用Struts的標簽庫信息。當然若開發人員使用了struts的標簽庫,也可以直接在jsp頁面中引入標簽庫,例如通過如下方式引入:

          <%@ 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中進行配置的的好處是因為可以在Struts中配置為tld文件配置一個簡要的名稱或者更加易懂的名稱,例如在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>元素指定標簽庫的相對或者絕對URI地址,Web應用將根據這一URI來訪問標簽庫;<taglib-location>元素指定標簽庫描述文件在文件資源系統中的物理位置。

                 此時在jsp頁面通過如下方面引入標簽庫:

          <%@ 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 完整配置實例

                 下面舉一個使用StrutsWeb項目的web.xml的簡單配置實例(該實例開發人員也參考struts-1.2.8-bin.zip包的webapps目錄下的struts-mailreader.war),內容如下所示:

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

           <!-- 錯誤處理 -->

           <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資源文件

                 默認情況下,Struts默認的資源文件為ApplicationResources.properties文件。

          資源文件可為一個束,可針對不同的語言,寫不同的資源文件,開發人員可以定義英文、簡體中文、繁體中文、日文等資源文件,資源文件的命名格式為:資源文件名_國別_語言.properties,常見的資源文件名稱為:

                 ApplicationResources.properties:默認資源文件,或英文資源文件

                 ApplicationResources_zh_CN.properties:簡體中文

                 ApplicationResources_zh_TW.properties:繁體中文

                 每一個資源文件都是“鍵-值”對的集合,例如可在src目錄下的資源文件ApplicationResources.properties中,編寫如下內容:

          UserForm.userName = USERNAME

          UserForm.password = PASSWORD

                 同時在src目錄下建立ApplicationResources_zh_CN.properties.bak文件來編寫簡體中文的原始資源文件,內容如下:

          UserForm.userName = 用戶名

          UserForm.password = 密碼

                 開發人員還可以建立ApplicationResources_zh_TW.properties.bak文件編寫繁體中文的原始資源文件。其內容如下:

          UserForm.userName = ノめ?W

          UserForm.password = ノめ?W

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

                 在實際項目中,一般編寫一個批處理文件(eg. code.bat)來處理資源文件的編碼,例如code.bat為如下內容時,可滿足要求將如上的ApplicationResources_zh_CN.properties.bak文件進行編碼,并將編碼后的信息放入ApplicationResources_zh_CN.properties文件中。該批處理文件的內容如下所示:

          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

                 運行code.bat之后,可看到目錄下多出了兩個資源文件,即:ApplicationResources_zh_CN.propertiesApplicationResources_zh_TW.properties。其中ApplicationResources_zh_CN.properties的內容是經過編碼的,內容如下:

          UserForm.userName = "u7528"u6237"u540d

          UserForm.password = "u5bc6"u7801

                 jsp頁面中,可以通過Struts的自定義標簽來獲取資源文件的某個鍵(例如:UserForm.userName)的信息。示例如下:

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

          參考文檔:
             

          《為Struts應用配置web.xml文件》

          Struts國際化處理》

           

          posted on 2008-01-05 12:23 阿蜜果 閱讀(9684) 評論(2)  編輯  收藏 所屬分類: Struts


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

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

                Power Designer系統分析與建模實戰  (2015年7月出版)
                
               Struts2+Hibernate3+Spring2   (2010年5月出版)
               

          留言簿(263)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          關注blog

          積分與排名

          • 積分 - 2296370
          • 排名 - 3

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 安顺市| 姜堰市| 洛扎县| 得荣县| 汕尾市| 乌海市| 乐都县| 开原市| 石阡县| 渭南市| 同仁县| 临西县| 阿巴嘎旗| 分宜县| 文登市| 柳林县| 军事| 长顺县| 湘乡市| 阿拉善右旗| 清流县| 和田县| 汉中市| 彭泽县| 略阳县| 呼伦贝尔市| 体育| 周宁县| 长白| 盐城市| 渝中区| 宁安市| 内乡县| 西贡区| 文成县| 嵊泗县| 宜丰县| 淮北市| 海城市| 鞍山市| 新闻|