posts - 297,  comments - 1618,  trackbacks - 0
                 struts-config.xmlStruts的主要配置文件,在該文件中,可以配置數(shù)據(jù)源、form-bean、actionplug-in(插件)和資源文件的信息。其文件主要結(jié)構(gòu)如下所示:

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

          <data-sources>

          <data-source>

          </data-source>

          </data-sources>

          <form-beans>

          <form-bean / >

          </form-beans>

          <global-forwards>

          <forward / >

          </global-forwards>

          <action-mappings>

          <action / >

          </action-mappings>

          <controller / >

          <message-resources / >

          <plug-in />
          </struts-config>

                 以上各元素必須是按照這個順序的,若開發(fā)人員打亂順序,很可能引起Struts容器啟動時出錯。

                 當(dāng)然struts-config.xml還有<display-name /><description /><icon />子元素,因為它們用得很少,在此不再贅述。只是講述常用的子元素的配置。

          1. data-sources

          本節(jié)講述子元素data-sources的配置,該元素可以配置一個或多個data-source元素,即數(shù)據(jù)源元素,可以通過<set-property>設(shè)置driverClassurl、user、password等屬性。配置實例如下:

          <data-source>

                                      <!-- 所用的JDBC驅(qū)動類,必須-->

                                      <set-property property="driverClass" value="com.mysql.jdbc.Driver"/>

                                      <!-- 所用的JDBCURL,必須-->

                                      <set-property property="url" value="jdbc:mysql://localhost/test"/>

                                      <!-- 同時打開的最小連結(jié)數(shù),缺省值為1,可選-->

                                      <set-property property="minCount" value="1"/>

                                      <!-- 同時打開的最大連結(jié)數(shù),缺省值為2,可選-->

                                      <set-property property="maxCount" value="5"/>

                                      <!-- 連結(jié)到數(shù)據(jù)庫的用戶名,必須-->

                                      <set-property property="user" value="root"/>

                                      <!-- 連結(jié)到數(shù)據(jù)庫的密碼,必須-->

                                      <set-property property="password" value="root"/>

                             </data-source>

          開發(fā)人員還可以設(shè)置Key(綁定在ServletContext上的DataSource實例的索引鍵,若不設(shè)定則缺省為Action.DATA_SOURCE_KEY,如果在應(yīng)用程序中有多于一個的DataSource,則必須設(shè)置Key的值)、Description(關(guān)于DataSource的描述信息)、ReadOnly(如果設(shè)為true,則表示該連結(jié)是只讀的,缺省為false)、LoginTimeout(創(chuàng)建連結(jié)的最大允許時間,以秒為單位)和AutoCommit(如果為true,則每次execute之后會強制回滾。缺省為true)屬性。

          在實際項目中,例如在Hibernate + Struts構(gòu)建的系統(tǒng)中,一般使用Hibernatehibernate.cfg.xml文件來配置數(shù)據(jù)源的信息。而在Hibernate + Struts + Spring構(gòu)建的系統(tǒng)中,一般使用spring的配置文件(eg. applicationContext.xml)來配置數(shù)據(jù)源的信息。

          2. form-beans

          子元素form-beans用來配置綁定到Action的各個FormBean的實例。每個FormBean實例用form-bans的子元素form-bean來定義。form-bean又分普通的FormBan和動態(tài)FormBean。

          1)普通form-bean

          普通FormBean需要定義一個JavaBean類,在form-bean元素中指定該類。普通form-bean元素的定義格式如下:

          <form-bean name="FormBean的名稱" type="FormBean對應(yīng)JavaBean類的全路徑"/>

          Eg. <form-bean name="UserForm"

                        type="com.amigo.struts.form.user.UserForm" />

          對應(yīng)的FormBean類一般是繼承ActionForm類,例如下面的例子定義了一個UserForm,它具有userNamepassword兩個屬性。該類的代碼如下:

          package com.amigo.struts.form.user;

          import org.apache.struts.action.ActionForm;

          public class UserForm extends ActionForm {

                   private static final long serialVersionUID = 1L;

                  

                   /** 用戶名.*/

                   private String userName;

                  

                   /** 密碼. */

                   private String password;

                   public String getPassword() {

                             return password;

                   }

                   public void setPassword(String password) {

                             this.password = password;

                   }

                   public String getUserName() {

                             return userName;

                   }

                   public void setUserName(String userName) {

                             this.userName = userName;

                   }

          }

          2)動態(tài)form-bean

                 動態(tài)form-bean不需要定義對應(yīng)的javabean類,其元素都在struts-config.xml中定義。其type為:org.apache.struts.validator.DynaValidatorForm。下面的動態(tài)FormBean定義了userNamepassword屬性,配置如下:

          <form-bean name="UserForm" type="org.apache.struts.validator.DynaValidatorForm">

                       <form-property name="userName" type="java.lang.String"/>

                       <form-property name="password" type="java.lang.String"/>

          </form-bean>

          3 global-forwards

                 global-forwards用于配置全局轉(zhuǎn)發(fā),struts首先會在<action-mappings>元素中找對應(yīng)的<forward>,若找不到,則到全局轉(zhuǎn)發(fā)配置中找。它包含0個或多個<forward/>元素,格式如下所示:

          <forward name="唯一的名稱" path="指向資源的相對路徑"/>

          Eg.

          <global-forwards>

                             <forward name="failed" path="/error.jsp" />

                             <forward name="success" path="/ success.jsp" />

          </global-forwards>

          <forward/>元素還有一個redirect屬性,其默認(rèn)值為false,如果redirect設(shè)為true的時候,則用HttpServletResponse.sendRedirect()方法,否則用RequestDispatcher.forward()方法,缺省為false。

          4 action-mappings

                 該元素用于將Action元素定義到ActionServlet類中,它含有0到多個<action/>元素,其格式如下:

          <action-mappings>

          <action path="Action請求的相對路徑"

          type="Action的對應(yīng)類的全路徑"

          name="Action綁定的FormBean"

          <forward name="指定處理相應(yīng)請求所對應(yīng)的地址" path="相對路徑"/>

          </action>

          </action-mappings>

                 每個action子元素可包含一個或多個forward子元素。除了path、typename屬性外,action還具有如下屬性:

          l         scope:指定ActionForm Bean的作用域(sessionrequest),缺省為session。(可選);

          l         input:當(dāng)Bean發(fā)生錯誤時返回的路徑(可選)

          l         classname:指定一個調(diào)用這個Action類的ActionMapping類的全名。缺省用org.apache.struts.action.ActionMapping(可選);

          l         include:如果沒有forward的時候,它起forward的作用(可選)

          l         validate:若為true,則會調(diào)用ActionFormvalidate()方法,否則不調(diào)用,缺省為true(可選)。

          forward屬性也是可選的。

          action元素定義舉例如下:

          Eg1.

          <action-mappings>

          <action

           path="/userAction"

           type="com.amigo.struts.action.UserAction"

           name="UserForm"

           scope="request"

           validate = "false"

           parameter="method" >

                       <forward name="error" path="/user/error.jsp" />

                   <forward name="success" path="/user/success.jsp"/>

                             <forward name="add" path="/user/addUser.jsp"/>

                             <forward name="update" path="/user/updateUser.jsp"/>

                             <forward name="list" path="/user/userList.jsp"/>

          </action>

          </action-mappings>

          Eg2. input屬性的例子:

          <action-mappings>

          <action path="/calcAction"

          type="com.amigo.struts.action.CalcAction"

          name="CalcForm"

          scope="request"

          validate="true"

          input="/index.jsp">

          <forward name="success" path="/success.jsp"/>
          <forward name="error" path="/error.jsp"/>

          </action>

          </action-mappings>

          Eg3. 僅有JSPaction元素:

          <action path="/menu"

          parameter="/default.jsp"

          type="org.apache.struts.actions.ForwardAction" />

          首先,ActionServlet接到請求后調(diào)用ForwardActionexecute()方法,execute()根據(jù)配置的parameter屬性值來forward到那個URI

          這樣做的效果是:沒有任何form被實例化,比較現(xiàn)實的情形可能是formrequest更高級別的范圍中定義;或者這個action被用作在應(yīng)用程序編譯好后充當(dāng)系統(tǒng)參數(shù),只需要更改這個配置文件而不需要重新編譯系統(tǒng)。

          5. message-resources

                 該元素用來定義資源文件,格式如下:

          <message-resources parameter="給定資源文件的全名"

          classname="定義處理消息資源的類名的全名"

          factory="定義MessageResourcesFactory類的全名"

          key="定義綁定在這個資源包中的ServletContext的屬性主鍵"

          null=" 如果為true,則找不到消息key時,則返回null "/>

                 message-resources的各屬性中,只有parameter是必選的,其余都為可選,classname屬性默認(rèn)為:org.apache.struts.config.MessageResourcesConfig,factory屬性默認(rèn)為:org.apache.struts.util.property.MessageResourcesFacotry,key屬性默認(rèn)為:Action.MESSAGES_KEYnull屬性默認(rèn)為:true

                 舉例如下,在struts配置文件中添加如下信息:

          Eg1. <message-resources parameter="ApplicationResources" />

          Eg2. <message-resources

           parameter="com.amigo.struts. ApplicationResources "

          null="false"/>

          6. plug-in

                 該元素用于定義插件,可定義0到多個插件元素,最常見的plug-inStruts的驗證的插件,配置舉例如下:

          Eg1. Struts的驗證的plug-in

          <plug-in className="org.apache.struts.validator.ValidatorPlugIn">

                   <set-property property="pathnames"

                             value="/WEB-INF/validator-rules.xml, /WEB-INF/manager/validation.xml" />

                   <set-property property="stopOnFirstError" value="false" />

          </plug-in>

          Eg2. Spring提供的載入插件配置:

          <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

          <set-property property="contextConfigLocation"

                        value="/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml"/>

           </plug-in>

          7. 完整配置實例

                 本小節(jié)舉例說明struts-config.xml文件的配置:

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

          <struts-config>

           <data-sources />

           <form-beans>

              <form-bean name="UserForm"

                             type="com.amigo.struts.form.user.UserForm" />

           </form-beans>

           

           <global-exceptions />

           <global-forwards />

           <action-mappings>

          <action

                          path="/userAction"

                          type="com.amigo.struts.action.UserAction"

                          name="UserForm"

                          scope="request"

                          validate = "false"

                          parameter="method" >

                       <forward name="error" path="/user/error.jsp" />

                            <forward name="success" path="/user/success.jsp"/>

                             <forward name="add" path="/user/addUser.jsp"/>

                             <forward name="update" path="/user/updateUser.jsp"/>

                             <forward name="list" path="/user/userList.jsp"/>

          </action>

          </action-mappings>

          <message-resources parameter="com.amigo.struts. ApplicationResources " />

          <plug-in className="org.apache.struts.validator.ValidatorPlugIn">

          <set-property property="pathnames"

          value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

          <set-property property="stopOnFirstError" value="false" /> 

          </plug-in>

          </struts-config>

              

            參考文章:

          struts-config.xml配置文件講解(一)》

          Struts-config.xml配置文件講解(二)》

          posted on 2008-01-03 09:23 阿蜜果 閱讀(10952) 評論(4)  編輯  收藏 所屬分類: Struts


          FeedBack:
          # re: 【Struts1.2總結(jié)系列】struts.config.xml配置
          2008-01-03 09:32 | ci
          不錯,很清楚..  回復(fù)  更多評論
            
          # re: 【Struts1.2總結(jié)系列】struts-config.xml配置
          2008-07-03 22:54 | wm
          謝謝
            回復(fù)  更多評論
            
          # re: 【Struts1.2總結(jié)系列】struts-config.xml配置[未登錄]
          2009-02-10 14:51 | stone
          文章寫得很好,很有幫助。  回復(fù)  更多評論
            
          # re: 【Struts1.2總結(jié)系列】struts-config.xml配置[未登錄]
          2009-02-19 15:39 | 笨笨
          很不錯,謝謝啦!  回復(fù)  更多評論
            
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

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

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

          留言簿(263)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          關(guān)注blog

          積分與排名

          • 積分 - 2296321
          • 排名 - 3

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 昆山市| 布尔津县| 新干县| 兴宁市| 道孚县| 武清区| 怀集县| 九寨沟县| 大方县| 拜泉县| 阳高县| 五家渠市| 沂南县| 五寨县| 仁化县| 牟定县| 兴仁县| 尉氏县| 乃东县| 临武县| 贵港市| 永宁县| 象州县| 白山市| 张掖市| 手游| 岳普湖县| 克拉玛依市| 静海县| 三台县| 霞浦县| 宝鸡市| 锡林浩特市| 开江县| 柏乡县| 宿州市| 堆龙德庆县| 昔阳县| 淮滨县| 桑日县| 涞源县|