更新時間:2021-11-12 12:03:28 來源:動力節點 瀏覽937次
web.xml 配置文件是一個 J2EE 配置文件,它決定了servlet容器如何處理 HTTP 請求的元素。它不是嚴格意義上的 Struts2 配置文件,而是 Struts2 需要配置才能工作的文件。
如前所述,該文件為任何 Web 應用程序提供了一個入口點。Struts2 應用程序的入口點將是部署描述符(web.xml)中定義的過濾器。因此,我們將在 web.xml 中定義FilterDispatcher類的條目 。需要在文件夾WebContent/WEB-INF下創建 web.xml 文件。
如果您在沒有生成它的模板或工具(例如 Eclipse 或 Maven2)的幫助下啟動,這是您需要配置的第一個配置文件。
以下是我們在上一個示例中使用的 web.xml 文件的內容。
<?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 過濾器解析。我們將在完成注釋章節時介紹這一點。
該在struts.xml文件中包含的配置信息,作為行動的開發,你會被修改。此文件可用于覆蓋應用程序的默認設置,例如struts.devMode = false和在屬性文件中定義的其他設置。該文件可以在文件夾WEB-INF/classes下創建。
讓我們看看我們在前一章解釋的 Hello World 示例中創建的 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> 是根標簽元素,在它下面我們使用 <package>標簽聲明不同的包。這里 <package>允許配置的分離和模塊化。當您有一個大型項目并且項目被劃分為不同的模塊時,這非常有用。
例如,如果您的項目具有三個域 - business_application、customer_application 和 staff_application,那么您可以創建三個包并將關聯的操作存儲在適當的包中。
該常數與名稱和值的屬性一起標簽應該被用來覆蓋任何定義下列屬性的default.properties,就像我們剛剛成立struts.devMode財產。設置struts.devMode屬性可以讓我們在日志文件中看到更多的調試信息。
我們定義了對應于我們想要訪問的每個 URL 的動作標簽,并且我們定義了一個帶有 execute() 方法的類,當我們訪問相應的 URL 時,它將被訪問。
結果決定了執行操作后返回給瀏覽器的內容。操作返回的字符串應該是結果的名稱。結果按上述配置,或作為“全局”結果,可用于包中的每個操作。結果具有可選的名稱和類型屬性。默認名稱值為“成功”。
Struts.xml 文件會隨著時間的推移而變大,因此通過包將其分解是模塊化的一種方法,但Struts提供了另一種模塊化 struts.xml 文件的方法。您可以將文件拆分為多個 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>
<include file="my-struts1.xml"/>
<include file="my-struts2.xml"/>
</struts>
我們沒有涉及的另一個配置文件是 struts-default.xml。該文件包含 Struts 的標準配置設置,您不必為 99.99% 的項目修改這些設置。出于這個原因,我們不會對此文件進行過多的詳細介紹。如果您有興趣,請查看struts2-core-2.2.3.jar 文件中的default.properties文件。
struts-config.xml 配置文件是 Web Client 中 View 和 Model 組件之間的鏈接,但您不必為 99.99% 的項目修改這些設置。
以下是示例 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>
此配置文件提供了一種更改框架默認行為的機制。實際上,struts.properties配置文件中包含的所有屬性也可以在web.xml 中使用init-param進行配置,也可以使用struts.xml配置文件中的 constant 標簽進行配置。但是,如果您希望將這些內容分開并更加具體,那么您可以在文件夾WEB-INF/classes下創建此文件。
此文件中配置的值將覆蓋struts2-core-xyzjar 發行版中包含的default.properties中配置的默認值。您可能會考慮使用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忽略。
通過上述相信大家對Struts2配置文件的方法已經有所了解,如果大家想了解更多相關知識,不妨來關注一下動力節點的Struts2視頻教程,課程內容詳細,通俗易懂,適合小白學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習