
- Struts 2 教程
- Struts 2 - 首頁
- Struts 2 - 基本 MVC 架構
- Struts 2 - 概述
- Struts 2 - 環境設定
- Struts 2 - 架構
- Struts 2 - 示例
- Struts 2 - 配置
- Struts 2 - Action
- Struts 2 - 攔截器
- Struts 2 - 結果型別
- Struts 2 - 值棧/OGNL
- Struts 2 - 檔案上傳
- Struts 2 - 資料庫訪問
- Struts 2 - 傳送郵件
- Struts 2 - 驗證
- Struts 2 - 本地化
- Struts 2 - 型別轉換
- Struts 2 - 主題/模板
- Struts 2 - 異常處理
- Struts 2 - 註解
- Struts 2 整合
- Struts 2 - Spring
- Struts 2 - Tiles
- Struts 2 - Hibernate
- Struts 2 有用資源
- Struts 2 - 問答
- Struts 2 - 快速指南
- Struts 2 - 有用資源
- Struts 2 - 討論
Struts 2 - 配置檔案
本章將引導您完成Struts 2應用程式所需的基本配置。在這裡,我們將瞭解如何使用一些重要的配置檔案(如web.xml、struts.xml、strutsconfig.xml和struts.properties)進行配置。
老實說,您只需使用web.xml和struts.xml配置檔案即可開始工作(正如您在上一章中已經看到的那樣,我們的示例使用了這兩個檔案)。但是,為了您的瞭解,我們也將解釋其他檔案。
web.xml 檔案
web.xml配置檔案是J2EE配置檔案,它確定servlet容器如何處理HTTP請求的元素。它並非嚴格意義上的Struts 2配置檔案,但它是Struts 2執行所需的一個檔案。
如前所述,此檔案為任何Web應用程式提供了一個入口點。Struts 2應用程式的入口點將在部署描述符(web.xml)中定義的過濾器。因此,我們將在web.xml中定義FilterDispatcher類的條目。web.xml檔案需要建立在WebContent/WEB-INF資料夾下。
如果您是從頭開始,沒有使用生成它的模板或工具(例如Eclipse或Maven 2),那麼這是您需要配置的第一個配置檔案。
以下是我們在上一個示例中使用的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.xml檔案包含您在開發Action時將修改的配置資訊。此檔案可用於覆蓋應用程式的預設設定,例如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配置檔案都需要具有正確的DOCTYPE,如我們的示例所示。
例如,如果您的專案有三個域——business_application、customer_application和staff_application,那麼您可以建立三個包並將相關的Action儲存在相應的包中。
package標籤具有以下屬性:
序號 | 屬性及描述 |
---|---|
1 | name (必需) 包的唯一識別符號 |
2 | extends 此包擴充套件自哪個包?預設情況下,我們使用struts-default作為基本包。 |
3 | abstract 如果標記為true,則該包不可用於終端使用者使用。 |
4 | namespace Action的唯一名稱空間 |
constant標籤與name和value屬性一起使用,可以覆蓋default.properties中定義的任何屬性,就像我們剛剛設定struts.devMode屬性一樣。設定struts.devMode屬性允許我們在日誌檔案中看到更多除錯資訊。
我們為每個要訪問的URL定義action標籤,並定義一個包含execute()方法的類,該方法將在我們訪問相應的URL時被訪問。
結果決定在執行Action後返回給瀏覽器的內容。Action返回的字串應該是結果的名稱。結果按上述方式為每個Action配置,或作為“全域性”結果,可用於包中的每個Action。結果具有可選的name和type屬性。預設名稱值為“success”。
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 檔案
struts-config.xml配置檔案是Web客戶端中檢視和模型元件之間的連結,但是對於99.99%的專案,您無需更改這些設定。
配置檔案基本上包含以下主要元素:
序號 | 攔截器及描述 |
---|---|
1 | struts-config 這是配置檔案的根節點。 |
2 | form-beans 在這裡,您可以將ActionForm子類對映到名稱。您在strutsconfig.xml檔案的其餘部分甚至在JSP頁面上都使用此名稱作為ActionForm的別名。 |
3 | global forwards 此部分將Web應用程式上的頁面對映到名稱。您可以使用此名稱來引用實際頁面。這避免了在網頁上硬編碼URL。 |
4 | action-mappings 在這裡,您可以宣告表單處理程式,它們也被稱為Action對映。 |
5 | controller 此部分配置Struts內部結構,在實際情況中很少使用。 |
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>
有關struts-config.xml檔案的更多詳細資訊,請檢視您的Struts文件。
Struts.properties 檔案
此配置檔案提供了一種更改框架預設行為的機制。實際上,struts.properties配置檔案中包含的所有屬性也可以在web.xml中使用init-param進行配置,也可以在struts.xml配置檔案中使用constant標籤進行配置。但是,如果您希望將這些內容分開,並使其更特定於Struts,則可以在WEB-INF/classes資料夾下建立此檔案。
在此檔案中配置的值將覆蓋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
這裡任何以hash (#) 開頭的行都將被視為註釋,並將被Struts 2忽略。