<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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è)配置文件中以為“,”隔開,如下所示:

    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>子元素中定義的文件是否存在,若存在,則將其返回給用戶,若不存在,繼續(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)配置跳轉(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工程沒有使用Struts的標(biāo)簽庫(kù),可以不在web.xml中使用Struts的標(biāo)簽庫(kù)信息。當(dāng)然若開發(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)要的名稱或者更加易懂的名稱,例如在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)訪問標(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í)例開發(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ǔ)言,寫不同的資源文件,開發(fā)人員可以定義英文、簡(jiǎn)體中文、繁體中文、日文等資源文件,資源文件的命名格式為:資源文件名_國(guó)別_語(yǔ)言.properties,常見的資源文件名稱為:

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

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

           ApplicationResources_zh_TW.properties:繁體中文

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

    UserForm.userName = USERNAME

    UserForm.password = PASSWORD

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

    UserForm.userName = 用戶名

    UserForm.password = 密碼

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

    UserForm.userName = ノめ?W

    UserForm.password = ノめ?W

           對(duì)于簡(jiǎn)體中文和繁體中文文件,開發(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)目中,一般編寫一個(gè)批處理文件(eg. code.bat)來(lái)處理資源文件的編碼,例如code.bat為如下內(nèi)容時(shí),可滿足要求將如上的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 阿蜜果 閱讀(9680) 評(píng)論(2)  編輯  收藏 所屬分類: 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ǒ愛伱--咾婆
    不是說(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)

    隨筆分類

    隨筆檔案

    文章分類

    相冊(cè)

    關(guān)注blog

    積分與排名

    • 積分 - 2294288
    • 排名 - 3

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲综合校园春色| 亚洲成a人片在线观看日本| 成人免费无遮挡无码黄漫视频| 久久国产精品成人片免费| 久久国产乱子伦精品免费不卡 | 精品亚洲视频在线观看| 亚洲精品国产V片在线观看| 亚洲成网777777国产精品| 亚洲熟女乱综合一区二区| 久久久精品国产亚洲成人满18免费网站| 久久精品国产精品亚洲下载| 亚洲人成色7777在线观看不卡| 国产精品亚洲小说专区| 亚洲欧洲av综合色无码| 亚洲av最新在线观看网址| 黄人成a动漫片免费网站| 国产区在线免费观看| 久久国产精品一区免费下载| 99久久99热精品免费观看国产| 亚洲免费观看在线视频| 免费无码肉片在线观看| 国产成人免费片在线视频观看| 亚洲av无码天堂一区二区三区 | 免费在线观看一级片| 久久成人国产精品免费软件| 毛片免费视频在线观看| 深夜国产福利99亚洲视频| 亚洲精品无码久久久久| 亚洲成人网在线观看| 在线观看亚洲电影| 青柠影视在线观看免费高清| 免费黄色网址网站| 国产极品美女高潮抽搐免费网站| 亚洲一级黄色视频| 亚洲一区二区三区四区在线观看| 亚洲制服丝袜第一页| 特黄特色的大片观看免费视频| 久久久精品免费视频| 破了亲妺妺的处免费视频国产| 久久久久亚洲AV成人网| 亚洲国产成+人+综合|