本章節(jié)將帶你學(xué)習(xí)Struts2 應(yīng)用程序所需的基本配置。在這里可以看到哪些將被配置到一些重要的配置文件中:web.xml、struts.xml、struts-config.xml以及struts.properties。
實際上,你可以繼續(xù)依賴于使用web.xml和struts.xml配置文件,并且你已經(jīng)在前面的章節(jié)中了解到,我們的示例是使用這兩個文件運作的,不過為了讓你了解更多,我們還是再來說明一下其他的文件。
正如前面所討論的,這個文件為每個web應(yīng)用程序提供接入點。在部署描述符(web.xml)中,Struts2 應(yīng)用程序的接入點將會定義為一個過濾器。因此我們將在web.xml里定義一個FilterDispatcher類的接入點,而這個web.xml文件需要在WebContent/WEB-INF文件夾下創(chuàng)建。
如果你開始時沒有模板或工具(比如Eclipse或Maven2)的輔助來生成,那這就是第一個你需要配置的文件。下面是我們在上一個例子中用到的web.xml的內(nèi)容。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
注意,我們將Struts2 過濾器映射到 /* ,而不是 /*.action ,這意味著所有的url都會被Struts過濾器解析。當(dāng)我們學(xué)到關(guān)于注解的章節(jié)時會對這個進(jìn)行討論。
注意:自2.1.3版本開始,ActionContextCleanUp和FilterDispatcher都由StrutsPrepareAndExecuteFilter代替。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> <-- more actions can be listed here --> </package> <-- more packages can be listed here --> </struts>
首先要注意的是DOCTYPE(文檔類型)。如我們的示例所示,所有的Struts配置文件都需要有正確的doctype。<struts>是根標(biāo)記元素,在其下,我們使用<package>標(biāo)簽聲明不同的包。 這里的<package>標(biāo)簽允許配置的分離和模塊化。這在你進(jìn)行一個大的項目并且項目分為多個不同的模塊時,是非常有用的。
如果您的項目有三個域:business_applicaiton、customer_application和staff_application的話,你可以創(chuàng)建三個包,并將相關(guān)的Actions存儲到相應(yīng)的包中。 <package>標(biāo)簽具有以下屬性:
屬性 | 描述 |
---|---|
name(必需) | 為package的唯一標(biāo)識 |
extends | 指定package繼承另一package的所有配置。通常情況下,我們使用struts-default作為package的基礎(chǔ)。 |
abstract | 定義package為抽象的。如果標(biāo)記為true,則package不能被最終用戶使用。 |
namespace | Actions的唯一命名空間 |
<constant>標(biāo)簽以及name和value屬性將用于覆蓋default.properties中定義的任一屬性,就像我們設(shè)置的struts.devMode屬性一樣。設(shè)置struts.devMode屬性允許我們在日志文件中查看更多的調(diào)試消息。
我們定義<action>標(biāo)簽對應(yīng)于我們想要訪問的每個URL,并且使用execute()方法定義一個訪問相應(yīng)的URL時將要訪問的類。
Results(結(jié)果)確定在執(zhí)行操作后返回到瀏覽器的內(nèi)容,而從操作返回的字符串應(yīng)該是結(jié)果的名稱。 Results按上述方式配置,或作為“全局”結(jié)果配置,可用于包中的每個操作。 Results有
name和
type屬性可選,默認(rèn)的name值是“success”。
Struts.xml文件可以隨著時間的推移而增長,因此通過包打破它是使它模塊化的一種方式,但struts提供了另一種模塊化struts.xml文件的方法,你可以將文件拆分為多個xml文件,并用以下方式導(dǎo)入它們。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="my-struts1.xml"/> <include file="my-struts2.xml"/> </struts>
我們沒有覆蓋的另一個配置文件是struts-default.xml。此文件包含Struts的標(biāo)準(zhǔn)配置設(shè)置,你不必再為99.99%的項目重復(fù)這些設(shè)置。 因此,我們不會深入了解這個文件的太多細(xì)節(jié)。如果你有興趣,可以查看在struts2-core-2.2.3.jar文件中可用的default.properties文件。
struts-config.xml配置文件是Web Client中View和Model組件之間的鏈接,但在你99.99%的項目里你不必使用這些設(shè)置。 struts-config.xml配置文件包含以下主要元素:
序號 | 攔截器和說明 |
---|---|
1 | struts-config
這是配置文件的根節(jié)點。 |
2 | form-beans
這是你將ActionForm子類映射到name的位置,你可以在struts-config.xml文件的其余部分,甚至在JSP頁面上,將這個name用作ActionForm的別名。 |
3 | global forwards
此部分將你在webapp上的頁面映射到name,你可以使用這個name來引用實際頁面。這避免了對你網(wǎng)頁上的URL進(jìn)行硬編碼。 |
4 | action-mappings
這是你聲明表單處理程序的地方,也被稱為操作映射(action mappings)。 |
5 | controller
這部分是配置Struts的內(nèi)部,在實際情況中很少使用。 |
6 | plug-in
這部分告訴Struts在哪里找到屬性文件,它包含提示和錯誤消息。 |
下面是struts-config.xml文件的示例:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <!-- ========== Form Bean Definitions ============ --> <form-beans> <form-bean name="login" type="test.struts.LoginForm" /> </form-beans> <!-- ========== Global Forward Definitions ========= --> <global-forwards> </global-forwards> <!-- ========== Action Mapping Definitions ======== --> <action-mappings> <action path="/login" type="test.struts.LoginAction" > <forward name="valid" path="/jsp/MainMenu.jsp" /> <forward name="invalid" path="/jsp/LoginView.jsp" /> </action> </action-mappings> <!-- ========== Controller Definitions ======== --> <controller contentType="text/html;charset=UTF-8" debug="3" maxFileSize="1.618M" locale="true" nocache="true"/> </struts-config>
有關(guān)struts-config.xml文件的更多詳細(xì)內(nèi)容,可查看Struts Documentation。
這個配置文件提供了一種機(jī)制來改變框架的默認(rèn)行為。實際上,struts.properties配置文件中包含的所有屬性也可以在web.xml中配置使用init-param,以及在struts.xml配置文件中使用constant標(biāo)簽。 但如果你想保持事件獨立以及保留更多struts細(xì)節(jié),那么你可以在WEB-INF/classes文件夾下創(chuàng)建這個文件。
struts.properties文件中配置的值將覆蓋
default.properties中配置的默認(rèn)值,這些值包含在struts2-core-x.y.z.jar分布中。有一些屬性,你可以考慮改為使用
struts.properties文件:
### When set to true, Struts will act much more friendly for developers struts.devMode = true ### Enables reloading of internationalization files struts.i18n.reload = true ### Enables reloading of XML configuration files struts.configuration.xml.reload = true ### Sets the port that the server is run on struts.url.http.port = 8080
這里任何以#(hash)開頭的行都將被假定為注釋,并且它會被Struts 2默認(rèn)忽略。
更多建議: