struts-config.xml

來源:互聯網
上載者:User

struts-config.xml是Struts的主要設定檔,在該檔案中,可以配置資料來源、form-bean、action和plug-in(外掛程式)和資源檔的資訊。其檔案主要結構如下所示:

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

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

       當然struts-config.xml還有<display-name />、<description />和<icon />子項目,因為它們用得很少,在此不再贅述。只是講述常用的子項目的配置。
1. data-sources

本節講述子項目data-sources的配置,該元素可以配置一個或多個data-source元素,即資料來源元素,可以通過<set-property>設定driverClass、url、user、password等屬性。配置執行個體如下:

<data-source>

                            <!-- 所用的JDBC驅動類,必須-->

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

                            <!-- 所用的JDBC的URL,必須-->

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

                            <!-- 同時開啟的最小連結數,預設值為1,可選-->

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

                            <!-- 同時開啟的最大連結數,預設值為2,可選-->

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

                            <!-- 連結到資料庫的使用者名稱,必須-->

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

                            <!-- 連結到資料庫的密碼,必須-->

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

                   </data-source>

開發人員還可以設定Key(綁定在ServletContext上的DataSource執行個體的索引鍵,若不設定則預設為Action.DATA_SOURCE_KEY,如果在應用程式中有多於一個的DataSource,則必須設定Key的值)、Description(關於DataSource的描述資訊)、ReadOnly(如果設為true,則表示該連結是唯讀,預設為false)、LoginTimeout(建立連結的最大允許時間,以秒為單位)和AutoCommit(如果為true,則每次execute之後會強制復原。預設為true)屬性。

在實際項目中,例如在Hibernate + Struts構建的系統中,一般使用Hibernate的hibernate.cfg.xml檔案來配置資料來源的資訊。而在Hibernate + Struts + Spring構建的系統中,一般使用spring的設定檔(eg. applicationContext.xml)來配置資料來源的資訊。
2. form-beans

子項目form-beans用來配置綁定到Action的各個FormBean的執行個體。每個FormBean執行個體用form-bans的子項目form-bean來定義。form-bean又分普通的FormBan和動態FormBean。

(1)普通form-bean

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

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

Eg. <form-bean name="UserForm"

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

對應的FormBean類一般是繼承ActionForm類,例如下面的例子定義了一個UserForm,它具有userName和password兩個屬性。該類的代碼如下:

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)動態form-bean

       動態form-bean不需要定義對應的javabean類,其元素都在struts-config.xml中定義。其type為:org.apache.struts.validator.DynaValidatorForm。下面的動態FormBean定義了userName和password屬性,配置如下:

<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用於配置全域轉寄,struts首先會在<action-mappings>元素中找對應的<forward>,若找不到,則到全域轉寄配置中找。它包含0個或多個<forward/>元素,格式如下所示:

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

Eg.

<global-forwards>

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

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

</global-forwards>

<forward/>元素還有一個redirect屬性,其預設值為false,如果redirect設為true的時候,則用HttpServletResponse.sendRedirect()方法,否則用RequestDispatcher.forward()方法,預設為false。
4 action-mappings

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

<action-mappings>

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

type="該Action的對應類的全路徑"

name="該Action綁定的FormBean"

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

</action>

</action-mappings>

       每個action子項目可包含一個或多個forward子項目。除了path、type和name屬性外,action還具有如下屬性:

l         scope:指定ActionForm Bean的範圍(session和request),預設為session。(可選);

l         input:當Bean發生錯誤時返回的路徑(可選);

l         classname:指定一個調用這個Action類的ActionMapping類的全名。預設用org.apache.struts.action.ActionMapping(可選);

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

l         validate:若為true,則會調用ActionForm的validate()方法,否則不調用,預設為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. 僅有JSP的action元素:

<action path="/menu"

parameter="/default.jsp"

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

首先,ActionServlet接到請求後調用ForwardAction的execute()方法,execute()根據配置的parameter屬性值來forward到那個URI。

這樣做的效果是:沒有任何form被執行個體化,比較現實的情形可能是form在request更進階別的範圍中定義;或者這個action被用作在應用程式編譯好後充當系統參數,只需要更改這個設定檔而不需要重新編譯系統。
5. message-resources

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

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

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

factory="定義MessageResourcesFactory類的全名"

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

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

       message-resources的各屬性中,只有parameter是必選的,其餘都為可選,classname屬性預設為:org.apache.struts.config.MessageResourcesConfig,factory屬性預設為:org.apache.struts.util.property.MessageResourcesFacotry,key屬性預設為:Action.MESSAGES_KEY,null屬性預設為:true。

       舉例如下,在struts設定檔中添加如下資訊:

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

Eg2. <message-resources

 parameter="com.amigo.struts. ApplicationResources "

null="false"/>
6. plug-in

       該元素用於定義外掛程式,可定義0到多個外掛程式元素,最常見的plug-in為Struts的驗證的外掛程式,配置舉例如下:

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. 完整配置執行個體

       本小節舉例說明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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.