一、????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配置文件必須按照這個(gè)順序進(jìn)行配置,否則在你的容器啟動(dòng)的時(shí)候就會(huì)出錯(cuò)。???<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的子元素:
1.<icon / >子元素
???它包含<small-icon / >及<large-icon / >,它的作用是圖形化其父元素,<small-icon/>的內(nèi)容是一個(gè)16x16的圖像文件,而<large-icon/>的內(nèi)容是一個(gè)32x32的圖像文件。如下例子:
<icon>
??<small-icon>
????/images/smalllogo.gif
??</small-icon>
??<large-icon>
????/images/largelogo.gif
??</large-icon>
</icon>
2.<display-name / >子元素??<small-icon>
????/images/smalllogo.gif
??</small-icon>
??<large-icon>
????/images/largelogo.gif
??</large-icon>
</icon>
???它提供對(duì)父元素的短文字(short textual)描述信息,如下:
<display-name>
???short?textual?discription?of?its?parent?element
</display-name>
???short?textual?discription?of?its?parent?element
</display-name>
3.<description / >子元素
???它提供對(duì)父元素的完全(full-length textual)的描述信息,如下:
<description>
???full-length?textual?discription?of?its?parent?element
</description>
???full-length?textual?discription?of?its?parent?element
</description>
4.<set-property / >子元素
???????它用來(lái)設(shè)置它的父元素中設(shè)定的JavaBean的屬性值,它一般用在指定的GenericDataSource 屬性,擴(kuò)展的ActionMappings以及擴(kuò)展的 global forwards。如下:
<set-property?
??property="name?of?bean?property"???????
value="value?of?bean?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"/>
<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數(shù)據(jù)源
其配置形式如下:
<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>的屬性及其描述信息如下:<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>
屬??性????描 述 信 息
Key????綁定在ServletContext上的DataSource實(shí)例的索引鍵,若不設(shè)定則缺省為Action.DATA_SOURCE_KEY,如果在應(yīng)用程序中有多于一個(gè)的DataSource,則必須設(shè)置Key的值。
DriverClass????所用的JDBC驅(qū)動(dòng)類(lèi)(必須的)如:com.microsoft.jdbc.sqlserver.SQLServerDriver
url????所用的JDBC的URL(必須的)如:jdbc:microsoft:sqlserver://xg088:1433
MaxCount????同時(shí)打開(kāi)的最大連結(jié)數(shù),缺省值為2(可選的)
MinCount????同時(shí)打開(kāi)的最小連結(jié)數(shù),缺省值為1(可選的)
User????連結(jié)到數(shù)據(jù)庫(kù)的用戶名(必須的)
Password????連結(jié)到數(shù)據(jù)庫(kù)的密碼(必須的)
Description????關(guān)于DataSource的描述信息(可選的)
ReadOnly????如果設(shè)為true,則表示該連結(jié)是只讀的,缺省為false。(可選的)
LoginTimeout????創(chuàng)建連結(jié)的最大允許時(shí)間,以秒為單位。(可選的)
AutoCommit????如果為true,則每次execute之后會(huì)強(qiáng)制回滾。缺省為true。(可選的)
舉例說(shuō)明:
<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????<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>
<form-bean / >用來(lái)定義將要綁定到Action的FormBean的實(shí)例。語(yǔ)法如下:
<form-beans>
???<form-bean?name="name?used?to?uniquely?identify?a?FormBean"?type="fully?qualified?class?name?of?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>
????<form-bean?name="lookupForm"?type="wiley.LookupForm"?/>
</form-beans>
五、????配置全局轉(zhuǎn)發(fā)
全局轉(zhuǎn)發(fā)可以定義幾個(gè)<forward/>子元素,struts首先會(huì)在<action-mappings>元素中找對(duì)應(yīng)的<forward>,若找不到,則到全局轉(zhuǎn)發(fā)配置中找。語(yǔ)法如下:
<global-forwards>
????<forward?name="unique?target?identifier"? path="context-relative?path?to?targetted?resource?"/>
</global-forwards>
除了name及path屬性之外,還有一個(gè)redirect屬性,如果redirect設(shè)為true的時(shí)候,則用HttpServletResponse.sendRedirect()方法,否則用RequestDispatcher.forward()方法,缺省為false。????<forward?name="unique?target?identifier"? path="context-relative?path?to?targetted?resource?"/>
</global-forwards>
注:如果為true,則用HttpServletResponse.sendRedirect()方法,此時(shí)存儲(chǔ)在原來(lái)的HttpServletRequest中的值將會(huì)丟失。
例子:
<global-forwards>
????<forward?name="success"?path="/welcome.jsp"/>
????<forward?name="failure"?path="/index.jsp"/>
</global-forwards>
????<forward?name="success"?path="/welcome.jsp"/>
????<forward?name="failure"?path="/index.jsp"/>
</global-forwards>
六、????配置<action-mappings>
它可以定義幾個(gè)<action / >子元素,它主要是定義Action實(shí)例到ActionServlet類(lèi)中,語(yǔ)法如下:
<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"/>
????<forward?name="forwardname2"?path="context-relative?path"/>
????</action>
</action-mappings>
<action/>屬性及其描述信息如下:????<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"/>
????<forward?name="forwardname2"?path="context-relative?path"/>
????</action>
</action-mappings>
屬??性????描 述 信 息
Path????在瀏覽器的URL中輸入的字符(必須的)
Type????連結(jié)到本映射的Action的全稱(可選的)
Name????與本操作關(guān)聯(lián)的Action Bean在<form-bean/>中定義name名(可選的)
Scope????指定ActionForm Bean的作用域(session和request),缺省為session。(可選的)
Input????當(dāng)Bean發(fā)生錯(cuò)誤時(shí)返回的控制。(可選的)
ClassName????指定一個(gè)調(diào)用這個(gè)Action類(lèi)的ActionMapping類(lèi)的全名。缺省用org.apache.struts.action.ActionMapping,(可選的)
Forward????指定處理相應(yīng)請(qǐng)求所對(duì)應(yīng)的JSP頁(yè)面。(可選的)
Include????如果沒(méi)有forward的時(shí)候,它起forward的作用。(可選的)
Validate????若為true,則會(huì)調(diào)用ActionForm的validate()方法,否則不調(diào)用,缺省為true。(可選的)
例子:
<action-mappings>
<action?path="/lookupAction" ?type="wiley.LookupAction" name="LookupForm" scope="request" validate="true" input="/index.jsp">
<forward?name="success"?path="/quote.jsp"/>
<forward?name="faliue"?path="/index.jsp"/>
</action>
</action-mappings>
<action?path="/lookupAction" ?type="wiley.LookupAction" name="LookupForm" scope="request" validate="true" input="/index.jsp">
<forward?name="success"?path="/quote.jsp"/>
<forward?name="faliue"?path="/index.jsp"/>
</action>
</action-mappings>
<action??path="/customer/customer"?//路徑的匹配模式??????????????????????????????????????
type="com.demo.order.actions.CustomerAction"???//滿足上面路徑模式的提交的請(qǐng)求有這個(gè)Action來(lái)響應(yīng),即有它來(lái)處理
name="customerForm"?//上面的路徑提交的數(shù)據(jù)存放在這個(gè)formBean給上面的Action處理
parameter="method"??
input="add"
unknown="false"
validate="true"?//把數(shù)據(jù)保存到FormBean時(shí)是否調(diào)用上面那個(gè)FormBean的validate函數(shù)進(jìn)行數(shù)據(jù)驗(yàn)證!
>
<forward??name="view"?path="model.customer.view" />?//Action處理完后通過(guò)Forward?name后面的字符串來(lái)跳轉(zhuǎn)到它相應(yīng)的path指定的頁(yè)面
<forward??name="add"?path="model.customer.add" />
<forward??name="list"?path="model.customer.list" />
</action>
七、????配置RequestProcessortype="com.demo.order.actions.CustomerAction"???//滿足上面路徑模式的提交的請(qǐng)求有這個(gè)Action來(lái)響應(yīng),即有它來(lái)處理
name="customerForm"?//上面的路徑提交的數(shù)據(jù)存放在這個(gè)formBean給上面的Action處理
parameter="method"??
input="add"
unknown="false"
validate="true"?//把數(shù)據(jù)保存到FormBean時(shí)是否調(diào)用上面那個(gè)FormBean的validate函數(shù)進(jìn)行數(shù)據(jù)驗(yàn)證!
>
<forward??name="view"?path="model.customer.view" />?//Action處理完后通過(guò)Forward?name后面的字符串來(lái)跳轉(zhuǎn)到它相應(yīng)的path指定的頁(yè)面
<forward??name="add"?path="model.customer.add" />
<forward??name="list"?path="model.customer.list" />
</action>
在struts-config.xml文件中用<controller/>子元素來(lái)定義RequestProcessor,其語(yǔ)法格式如下:
<controller processorClass="fully qualified class name" />
<controller />元素屬性及其描述信息如下:
屬??性????描??述
processorClass????指定自定義的RequestProcessor類(lèi)的全名
BufferSize????指定用來(lái)下載所用的緩存大小。缺省是4096字節(jié)。
contentType????定義response文本類(lèi)型,缺省是text/html
Debug????定義當(dāng)前系統(tǒng)的除錯(cuò)級(jí)別,缺省是0
Locale????如果是true,則在用戶的session中存放Locale對(duì)象,缺省為true
maxFileSize????指定下載文件最大的大小。缺省是250M
multipartClass????指定去代替org.apache.struts.upload.DiskMultipartRequestHandler類(lèi)的類(lèi)的全名。
Nocache????如果是true,則會(huì)關(guān)閉每個(gè)response的緩存功能。缺省是false
TempDir????指定上載文件所用的臨時(shí)目錄。缺省值由容器決定
例子:
①?<controller?processorClass="wiley.WileyRequestProcessor"?/>
②?<controller
????contentType="text/html;charset=UTF-8"
????debug="3"
????locale="true"
????nocache="true"
????processorClass="org.apache.struts.action.RequestProcessor"/>
②?<controller
????contentType="text/html;charset=UTF-8"
????debug="3"
????locale="true"
????nocache="true"
????processorClass="org.apache.struts.action.RequestProcessor"/>
八、????配置Message Resources
在struts-config.xml文件中用<message-resources />元素來(lái)定義消息資源。其語(yǔ)法如下:
<message-resources??parameter="wiley.ApplicationResources"/>
<message-resources?/>
元素屬性及其描述信息如下:<message-resources?/>
屬??性????描??述
Parameter????給定資源文件全名
ClassName????定義處理消息資源的類(lèi)名的全名,缺省是org.apache.struts.config.MessageResourcesConfig
Factory????定義MessageResourcesFactory類(lèi)的全名,缺省是org.apache.struts.util.property.MessageResourcesFacotry
Key????定義綁定在這個(gè)資源包中的ServletContext的屬性主鍵,缺省值是Action.MESSAGES_KEY.
Null????如果為true,則找不到消息key時(shí),則返回null,缺省是true.
例子:
①?<message-resources?parameter="wiley.ApplicationResources"/>
②?<message-resources
????parameter="StorefrontMessageResources"
????null="false"/>
<message-resources
????key="IMAGE_RESOURCE_KEY"
????parameter="StorefrontImageResources"
????null="false"/>
②?<message-resources
????parameter="StorefrontMessageResources"
????null="false"/>
<message-resources
????key="IMAGE_RESOURCE_KEY"
????parameter="StorefrontImageResources"
????null="false"/>
注意:設(shè)定key的目的如下:
<html:img?altKey="navbar.home.image.alt"?bundle="IMAGE_RESOURCE_KEY"?pageKey="navbar.home.image"?width="125"?height="15"?border="0"/>
這里說(shuō)明要到StorefrontImageResources.properties資源文件中找主鍵值是”navbar.home.image”所對(duì)應(yīng)的值。這里StorefrontImageResources.properties的內(nèi)容如下:
……
navbar.home.image=/images/home.gif
navbar.home.image.alt=Home
……
此處navbar.home.image.alt說(shuō)明的和<img alt=”Home”……/>一樣。
九、????配置Plug-in
詳細(xì)學(xué)習(xí)可參考:http://www.aygfsteel.com/yesjoy/articles/106419.html
配置Plug-in如下:
<plug-in?className="wiley.WileyPlugin"/>
也可如下:<plug-in?className="com.oreilly.struts.storefront.service.memory.StorefrontMemoryDatabasePlugIn">
??<set-property?property="pathname"?value="/WEB-INF/database.xml"/>
</plug-in>
??<set-property?property="pathname"?value="/WEB-INF/database.xml"/>
</plug-in>