Struts-config.xml設定檔講解(一)

來源:互聯網
上載者:User
Struts的核心是struts-config.xml設定檔,在這個檔案裡描述了所有的Struts組件。在這裡包括配置主要的組件及次要的組件,下面是struts-config.xml包含主要元素的內容:

一、    struts-config.xml的主要元素:
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
   
   <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-config.xml設定檔必須按照這個順序進行配置,否則在你的容器啟動的時候就會出錯。

二、    struts-config.xml的子項目:
1.<icon / >子項目
   它包含<small-icon / >及<large-icon / >,它的作用是圖形化其父元素,<small-icon/>的內容是一個16x16的影像檔,而<large-icon/>的內容是一個32x32的影像檔。如下例子:
   <icon>
<small-icon>
              /images/smalllogo.gif
</small-icon>
<large-icon>
    /images/largelogo.gif
</large-icon>
</icon>
2.<display-name / >子項目
   它提供對父元素的短文字(short textual)描述資訊,如下:
   <display-name>
           short textual discription of its parent element
   </display-name>
3.<description / >子項目
   它提供對父元素的完全(full-length textual)的描述資訊,如下:
<description>
full-length textual discription of its parent element
</description>
4.<set-property / >子項目
       它用來設定它的父元素中設定的JavaBean的屬性值,它一般用在指定的GenericDataSource 屬性,擴充的ActionMappings以及擴充的 global forwards。如下:
       <set-property
           property="name of bean property"       
value="value of bean property" />
         例如:
         <set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
        <set-property property="user" value="admin"/>
        <set-property property="maxCount" value="4"/>
<set-property property="minCount" value="2"/>
<set-property property="password" value=""/>
<set-property property="url" value="jdbc:mysql://localhost:3306/struts"/>

三、    配置JDBC資料來源
其配置形式如下:
<data-sources>
<data-source>
<set-property property="driverClass" value="fully qualified path of JDBC driver"/>
<set-property property="url" value="data source URL"/>
<set-property property=”mincount” value="the minimum number of connections to open"/>
<set-property property="password" value="the password used to create connections"/>
<set-property property="user" value="the username used to create connections"/>
</data-source>
</data-sources>
<data-source>的屬性及其描述資訊如下:
屬  性    描 述 信 息
Key    綁定在ServletContext上的DataSource執行個體的索引鍵,若不設定則預設為Action.DATA_SOURCE_KEY,如果在應用程式中有多於一個的DataSource,則必須設定Key的值。
DriverClass    所用的JDBC驅動類(必須的)如:com.microsoft.jdbc.sqlserver.SQLServerDriver
url    所用的JDBC的URL(必須的)如:jdbc:microsoft:sqlserver://xg088:1433
MaxCount    同時開啟的最大連結數,預設值為2(可選的)
MinCount    同時開啟的最小連結數,預設值為1(可選的)
User    連結到資料庫的使用者名稱(必須的)
Password    連結到資料庫的密碼(必須的)
Description    關於DataSource的描述資訊(可選的)
ReadOnly    如果設為true,則表示該連結是唯讀,預設為false。(可選的)
LoginTimeout    建立連結的最大允許時間,以秒為單位。(可選的)
AutoCommit    如果為true,則每次execute之後會強制復原。預設為true。(可選的)
舉例說明:
<data-sources>
    <data-source>
        <set-property property=”key” value=” value="WILEY_DATA_SOURCE" />
<set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/wileyusers" />
<set-property property="maxCount" value="5"/>
<set-property property="minCount" value="1"/>
<set-property property="user" value="sa"/>
<set-property property="password" value="yourpassword"/>
</data-source>
</data-sources>

四、    配置FormBean
<form-bean / >用來定義將要綁定到Action的FormBean的執行個體。文法如下:
<form-beans>
<form-bean name="name used to uniquely identify a FormBean"
            type=”fully qualified class name of FormBean"/>
         </form-beans>
例:
<form-beans>
<form-bean name="lookupForm" type="wiley.LookupForm" />
</form-beans>

五、    配置全域轉寄
全域轉寄可以定義幾個<forward/>子項目,struts首先會在<action-mappings>元素中找對應的<forward>,若找不到,則到全域轉寄配置中找。文法如下:
<global-forwards>
<forward name="unique target identifier"
path="context-relative path to targetted resource "/>
</global-forwards>
除了name及path屬性之外,還有一個redirect屬性,如果redirect設為true的時候,則用HttpServletResponse.sendRedirect()方法,否則用RequestDispatcher.forward()方法,預設為false。
註:如果為true,則用HttpServletResponse.sendRedirect()方法,此時儲存在原來的HttpServletRequest中的值將會丟失。
例子:
<global-forwards>
<forward name="success" path="/welcome.jsp"/>
<forward name="failure" path="/index.jsp"/>
</global-forwards>
六、    配置<action-mappings>
它可以定義幾個<action / >子項目,它主要是定義Action執行個體到ActionServlet類中,文法如下:
<action-mappings>
<action path="context-relative path mapping action to a request"
type="fully qualified class name of the Action class"
name="the name of the form bean bound to this Action">
<forward name="forwardname1" path="context-relative path"/>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.