更新時(shí)間:2021-11-12 12:03:28 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1001次
web.xml 配置文件是一個(gè) J2EE 配置文件,它決定了servlet容器如何處理 HTTP 請求的元素。它不是嚴(yán)格意義上的 Struts2 配置文件,而是 Struts2 需要配置才能工作的文件。
如前所述,該文件為任何 Web 應(yīng)用程序提供了一個(gè)入口點(diǎn)。Struts2 應(yīng)用程序的入口點(diǎn)將是部署描述符(web.xml)中定義的過濾器。因此,我們將在 web.xml 中定義FilterDispatcher類的條目 。需要在文件夾WebContent/WEB-INF下創(chuàng)建 web.xml 文件。
如果您在沒有生成它的模板或工具(例如 Eclipse 或 Maven2)的幫助下啟動(dòng),這是您需要配置的第一個(gè)配置文件。
以下是我們在上一個(gè)示例中使用的 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,這意味著所有 url 都將由 struts 過濾器解析。我們將在完成注釋章節(jié)時(shí)介紹這一點(diǎn)。
該在struts.xml文件中包含的配置信息,作為行動(dòng)的開發(fā),你會(huì)被修改。此文件可用于覆蓋應(yīng)用程序的默認(rèn)設(shè)置,例如struts.devMode = false和在屬性文件中定義的其他設(shè)置。該文件可以在文件夾WEB-INF/classes下創(chuà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 配置文件都需要有正確的文檔類型,如我們的小例子所示。<struts> 是根標(biāo)簽元素,在它下面我們使用 <package>標(biāo)簽聲明不同的包。這里 <package>允許配置的分離和模塊化。當(dāng)您有一個(gè)大型項(xiàng)目并且項(xiàng)目被劃分為不同的模塊時(shí),這非常有用。
例如,如果您的項(xiàng)目具有三個(gè)域 - business_application、customer_application 和 staff_application,那么您可以創(chuàng)建三個(gè)包并將關(guān)聯(lián)的操作存儲在適當(dāng)?shù)陌小?/p>
該常數(shù)與名稱和值的屬性一起標(biāo)簽應(yīng)該被用來覆蓋任何定義下列屬性的default.properties,就像我們剛剛成立struts.devMode財(cái)產(chǎn)。設(shè)置struts.devMode屬性可以讓我們在日志文件中看到更多的調(diào)試信息。
我們定義了對應(yīng)于我們想要訪問的每個(gè) URL 的動(dòng)作標(biāo)簽,并且我們定義了一個(gè)帶有 execute() 方法的類,當(dāng)我們訪問相應(yīng)的 URL 時(shí),它將被訪問。
結(jié)果決定了執(zhí)行操作后返回給瀏覽器的內(nèi)容。操作返回的字符串應(yīng)該是結(jié)果的名稱。結(jié)果按上述配置,或作為“全局”結(jié)果,可用于包中的每個(gè)操作。結(jié)果具有可選的名稱和類型屬性。默認(rèn)名稱值為“成功”。
Struts.xml 文件會(huì)隨著時(shí)間的推移而變大,因此通過包將其分解是模塊化的一種方法,但Struts提供了另一種模塊化 struts.xml 文件的方法。您可以將文件拆分為多個(gè) 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>
我們沒有涉及的另一個(gè)配置文件是 struts-default.xml。該文件包含 Struts 的標(biāo)準(zhǔn)配置設(shè)置,您不必為 99.99% 的項(xiàng)目修改這些設(shè)置。出于這個(gè)原因,我們不會(huì)對此文件進(jìn)行過多的詳細(xì)介紹。如果您有興趣,請查看struts2-core-2.2.3.jar 文件中的default.properties文件。
struts-config.xml 配置文件是 Web Client 中 View 和 Model 組件之間的鏈接,但您不必為 99.99% 的項(xiàng)目修改這些設(shè)置。
以下是示例 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>
此配置文件提供了一種更改框架默認(rèn)行為的機(jī)制。實(shí)際上,struts.properties配置文件中包含的所有屬性也可以在web.xml 中使用init-param進(jìn)行配置,也可以使用struts.xml配置文件中的 constant 標(biāo)簽進(jìn)行配置。但是,如果您希望將這些內(nèi)容分開并更加具體,那么您可以在文件夾WEB-INF/classes下創(chuàng)建此文件。
此文件中配置的值將覆蓋struts2-core-xyzjar 發(fā)行版中包含的default.properties中配置的默認(rèn)值。您可能會(huì)考慮使用struts.properties文件更改幾個(gè)屬性
### 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 (#)開頭的行都將被假定為注釋,它會(huì)被Struts 2忽略。
通過上述相信大家對Struts2配置文件的方法已經(jīng)有所了解,如果大家想了解更多相關(guān)知識,不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的Struts2視頻教程,課程內(nèi)容詳細(xì),通俗易懂,適合小白學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743