本章將帶你學(xué)習(xí)一個 Struts 2 應(yīng)用程序必需的基本配置。在這里,我們將看到一些重要的配置文件:web.xml,struts.xml,struts-config.xml 和 struts.properties,它們將被配置。
老實(shí)說你可以使用 web.xml 和 struts.xml 配置文件,在前面的章節(jié)中你已經(jīng)看到了我們的例子使用這兩個文件進(jìn)行工作,但是為了你學(xué)習(xí)知識,我也解釋其他文件。
web.xml 配置文件是一個 J2EE 的配置文件,它決定如何用 servlet 容器來處理 HTTP 請求的元素。它不是嚴(yán)格意義上的一個 Struts 2 的配置文件,但它是一個 Struts 2 工作時需要被配置的文件。
如前所述,這個文件為任何 web 應(yīng)用程序提供了一個入口點(diǎn)。Struts 2 應(yīng)用程序的入口點(diǎn)是一個在部署描述符(web.xml)中已定義的過濾器。因此,我們將在 web.xml 中定義 FilterDispatcher 類的入口。web.xml 文件需要在 WebContent/WEB-INF 文件夾下創(chuàng)建。
如果你在沒有生成配置文件的模板或工具(如 Eclipse 或 Maven2)的幫助下開始,那么 web.xml 是你需要配置的第一個配置文件。下面是在我們最后一個例子中使用的 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>
注意,我們映射 Struts 2 的過濾器為 /*,而不是 */.action**,這意味著所有的 urls 將被 struts 的過濾器解析。當(dāng)我們進(jìn)入注解這一章時,我們將討論這個。
struts.xml 文件包含配置信息,隨著動作的開發(fā),你將會修改這些配置信息。這個文件可以用來重寫應(yīng)用程序的默認(rèn)設(shè)置,例如 struts.devMode = false,還有定義在屬性文件中的其他設(shè)置。這個文件可以在文件夾 WEB-INF/classes 下創(chuàng)建。
讓我們來看看在前面的章節(jié)中已經(jīng)解釋的 Hello World 例子中創(chuàng)建的 struts.xml 文件。
<?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="com.tutorialspoint.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,正如我們的小例子所示。
也就是說,如果你的項(xiàng)目有三個域 - business_applicaiton,customer_application 和 staff_application,你可以創(chuàng)建三個包,并且在適當(dāng)?shù)陌写鎯ο嚓P(guān)的動作。包標(biāo)簽具有以下的屬性:
屬性 | 描述 |
---|---|
name (required) | 包的唯一標(biāo)識符。 |
extends | 這個包是由哪個包擴(kuò)展的?默認(rèn)情況下,我們使用 struts-default 作為基礎(chǔ)包。 |
abstract | 如果標(biāo)記為 true,對于終端用戶消費(fèi)來說,這個包是不可用的。 |
namesapce | 動作的唯一命名空間。 |
帶著 name 和 value 屬性的常量標(biāo)簽將被用于重寫任何 default.properties 中定義下面的屬性,就如我們剛剛設(shè)置 struts.devMode 屬性的過程。設(shè)置 struts.devMode 屬性允許我們在日志文件中看到更多的調(diào)試信息。
我們定義對應(yīng)于每一個我們要訪問的 URL 的動作標(biāo)簽,我們定義了一個帶有 execute() 方法的類,每當(dāng)我們訪問相應(yīng)的 URL 時,它將被訪問。
在動作被執(zhí)行后,結(jié)果決定把得到的哪些返回給瀏覽器。從動作中返回的字符串應(yīng)該是一個結(jié)果的名稱。同上,每次執(zhí)行動作,結(jié)果都被配置,或者都作為一個 “global” 的結(jié)果,包中的每個動作都是可用的。結(jié)果有可選的 name 和 type 屬性。默認(rèn)名稱的值為 “success”。
隨著時間的推移,Struts.xml 文件可以擴(kuò)展,用包阻止它是一種使它模塊化的方式,但是 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è)置,你就不必要為了你的項(xiàng)目的 99.99% 修改這些設(shè)置。為此,我們不打算詳細(xì)地介紹這個文件。如果你有興趣,可以在 struts2-core-2.2.3.jar 文件中有效的 default.properties 文件里看到它。
struts-config.xml 配置文件是在 Web 客戶端中視圖和模型組件之間的鏈接,但是你就不必要為了你的項(xiàng)目的 99.99% 而修改這些設(shè)置。基本配置文件包含下面的主要內(nèi)容:
序號 | 攔截器 & 描述 |
---|---|
1 |
struts-config
它是配置文件的根節(jié)點(diǎn)。 |
2 |
form-beans
它是你把 ActionForm 子類映射到名稱上的位置。你使用這個名字作為 ActionForm 的別名,貫穿 struts-config.xml 的其余部分,甚至在 JSP 頁面中。 |
3 |
global forwards
這個部分把 web 應(yīng)用的頁面映射到名稱上。你可以使用該名稱來引用實(shí)際的頁面。這個避免了在 web 頁面上硬編碼 URLs。 |
4 |
action-mappings
它是你聲明表單處理程序的位置,他們也被稱為動作映射。 |
5 |
controller
這個部分配置 Struts 內(nèi)部,而且很少在實(shí)際情況中使用。 |
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ì)信息,請查看你的 struts 文檔。
這個配置文件提供了一種改變框架的默認(rèn)行為的機(jī)制。實(shí)際上,包含在 struts.properties 配置文件內(nèi)的所有屬性也可以在 web.xml 中使用 init-param 被配置,同樣也可以在 struts.xml 配置文件中使用 constant 標(biāo)簽。但是如果你喜歡保持事情分離和有更多特定的 struts,你就可以在文件夾 WEB-INF/classes 下創(chuàng)建這個文件。
在這個文件中配置的值將重寫在 default.properties 中配置的默認(rèn)值,default.properties 包含在 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
在這里,任何以井號(#)開始的行會被假定為注釋,它將被 Struts 2 忽略。