struts.xml是我們在開發中利用率最高的文件,也是Struts2中最重要的配置文件。
一下分別介紹一下幾個struts.xml中常用到的標籤
1、<include>
利用include標籤,可以將一個struts.xml配置文件分割成多個配置文件,然後在struts.xml中使用<include>標籤引入其他配置文件。
比如一個網上購物程序,可以把用戶配置、商品配置、訂單配置分別放在3個配置文件user.xml、goods.xml和order.xml中,然後在struts.xml中將這3個配置文件引入:
struts.xml:
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < include file = "user.xml" /> < include file = "goods.xml" /> < include file = "order.xml" /> </ struts > |
user.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "wwfy" extends = "struts-default" > < action name = "login" class = "wwfy.user.LoginAction" > <!--省略Action其他配置--> </ action > < action name = "logout" class = "wwfy.user.LogoutAction" > <!--省略Action其他配置--> </ action > </ package > </ struts > |
2、<constant>
在之前提到struts.properties配置文件的介紹中,我們曾經提到所有在struts.properties文件中定義的屬性,都可以配置在struts.xml文件中。而在struts.xml中,是通過<constant>標籤來進行配置的:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > <!--設置開發模式--> < constant name = "struts.devMode" value = "true" /> <!--設置編碼形式爲GB2312--> < constant name = "struts.i18n.encoding" value = "GB2312" /> <!--省略其他配置信息--> </ struts > |
3、<package>
1、包屬性介紹
在Struts2框架中是通過包來管理action、result、interceptor、interceptor-stack等配置信息的。包屬性如下:
屬性 |
是否必需 |
描述 |
name | 是 | 包名,作爲其它包應用本包的標記 |
extends | 否 | 設置本包繼承其它包 |
namespace | 否 | 設置包的命名空間 |
abstact | 否 | 設置爲抽象包 |
2、extends屬性的詳解
- 當一個包通過配置extends屬性繼承了另一個包的時候,該包將會繼承父包中所有的配置,包括action、result、interceptor等。
- 由於包信息的獲取是按照配置文件的先後順序進行的,所以父包必須在子包之前被定義。
- 通常我們配置struts.xml的時候,都繼承一個名爲“struts-default.xml”的包,這是struts2中內置的包。
3、namespace的詳解
namespace主要是針對大型項目中Action的管理,更重要的是解決Action重名問題,因爲不在同一個命名空間的Action可以使用相同的Action名的。
1)如果使用命名空間則URL將改變
比如我們有一下配置文件
1
2
3
4
5
|
< package name = "wwfy" extends = "struts-default" > < action name = "login" class = "wwfy.action.LoginAction" > < result >/success.jsp</ result > </ action > </ package > |
則此配置下的Action的URL爲http://localhost:8080/login.action
假如爲這個包指定了命名空間
1
2
3
4
5
|
< package name = "wwfy" extends = "struts-default" namespace = "/user" > < action name = "login" class = "wwfy.action.LoginAction" > < result >/success.jsp</ result > </ action > </ package > |
則此配置下的Action的URL爲http://localhost:8080/user/login.action
2)默認命名空間
Struts2中如果沒有爲某個包指定命名空間,該包使用默認的命名空間,默認的命名空間總是""。
3)指定根命名空間
當設置了命名空間爲“/”,即指定了包的命名空間爲根命名空間時,此時所有根路徑下的Action請求都會去這個包中查找對應的資源信息。
假若前例中路徑爲http://localhost:8080/login.action則所有http://localhost:8080/*.action都會到設置爲根命名空間的包中尋找資源。
4、<action>與<result>
1、<action>屬性介紹
屬性名稱 |
是否必須 |
功能描述 |
name | 是 | 請求的Action名稱 |
class | 否 | Action處理類對應具體路徑 |
method | 否 | 指定Action中的方法名 |
converter | 否 | 指定Action使用的類型轉換器 |
如果沒有指定method則默認執行Action中的execute方法。
2、<result>屬性介紹
屬性名稱 |
是否必須 |
功能描述 |
name | 否 | 對應Action返回邏輯視圖名稱,默認爲success |
type | 否 | 返回結果類型,默認爲dispatcher |
3、通配符的使用
隨着result的增加,struts.xml文件也會隨之變得越來越複雜。那麼就可以使用通配符來簡化配置:
例如下面這個案例:
Action爲Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Test
{ public String
test1(){ return "result1" ; } public String
test2(){ return "result2" ; } public String
test3(){ return "result3" ; } } |
struts.xml中配置爲
1
2
3
4
5
|
< package name = "wwfy" extends = "struts-default" > < action name = "test*" class = "wwfy.action.test{1}" > < result name = "result{1}" >/result{1}.jsp</ result > </ action > </ package > |
4、訪問Action方法的另一種實現方式
在Struts2中如果要訪問Action中的指定方法,還可以通過改變URL請求來實現,將原本的“Action名稱.action”改爲“Action名稱!方法名稱.action”在struts.xml中就不需要指定方法名了。
5、<exception-mapping>與<global-exception-mapping>
這兩個標籤都是用來配置發生異常時對應的視圖信息的,只不過一個是Action範圍的,一個是包範圍的,當同一類型異常在兩個範圍都被配置時,Action範圍的優先級要高於包範圍的優先級.這兩個標籤包含的屬性也是一樣的:
屬性名稱 |
是否必須 |
功能描述 |
name | 否 | 用來表示該異常配置信息 |
result | 是 | 指定發生異常時顯示的視圖信息,這裏要配置爲邏輯視圖 |
exception | 是 | 指定異常類型 |
兩個標籤的示例代碼爲:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "default" extends = "struts-default" > < global-exception-mappings > < exception-mapping result = "邏輯視圖" exception = "異常類型" /> </ global-exception-mappings > < action name = "Action名稱" > < exception-mapping result = "邏輯視圖" exception = "異常類型" /> </ action > </ package > </ struts > |
6、<default-class-ref>
當我們在配置Action的時候,如果沒有爲某個Action指定具體的class值時,系統將自動引用<default-class-ref>標籤中所指定的類。在Struts2框架中,系統默認的class爲ActionSupport,該配置我們可以在xwork的核心包下的xwork-default.xml文件中找到。
有特殊需要時,可以手動指定默認的class
1
2
3
4
5
6
7
|
package wwfy.action; public class DefaultClassRef
{ public void execute(){ System.out.println( "默認class開始執行……" ); } } |
在struts.xml中配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "wwfy" extends = "struts-default" > <!--
指定默認class爲Test --> < default-class-ref class = "wwfy.action.DefaultClassRef" /> < action name = "test1" > < result >/index.jsp</ result > </ action > </ package > </ struts > |
7、<default-action-ref>
如果在請求一個沒有定義過的Action資源時,系統就會拋出404錯誤。這種錯誤不可避免,但這樣的頁面並不友好。我們可以使用<default-action-ref>來指定一個默認的Action,如果系統沒有找到指定的Action,就會指定來調用這個默認的Action。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "wwfy" extends = "struts-default" > < default-action-ref name = "acctionError" ></ default-action-ref > < action name = "acctionError" > < result >/jsp/actionError.jsp</ result > </ action > </ package > </ struts > |
8、<default-interceptor-ref>
該標籤用來設置整個包範圍內所有Action所要應用的默認攔截器信息。事實上我們的包繼承了struts-default包以後,使用的是Struts的默認設置。我們可以在struts-default.xml中找到相關配置:
1
|
< default-interceptor-ref name = "defaultStack" /> |
在實際開發過程中,如果我們有特殊的需求是可以改變默認攔截器配置的。當時一旦更改這個配置,“defaultStack”將不再被引用,需要手動最加。
9、<interceptors>
通過該標籤可以向Struts2框架中註冊攔截器或者攔截器棧,一般多用於自定義攔截器或攔截器棧的註冊。該標籤使用方法如下:
1
2
3
4
5
6
|
< interceptors > < interceptor name = "攔截器名" class = "攔截器類" /> < interceptor-stack name = "攔截器棧名" > < interceptor-ref name = "攔截器名" > </ interceptor-stack > </ interceptors > |
10、<interceptor-ref>
通過該標籤可以爲其所在的Action添加攔截器功能。當爲某個Action單獨添加攔截器功能後,<default-interceptor-ref>中所指定的攔截器將不再對這個Action起作用。
11、<global-results>
該標籤用於設置包範圍內的全局結果集。在多個Action返回相同邏輯視圖的情況下,可以通過<global-results>標籤統一配置這些物理視圖所對應的邏輯視圖。
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache
Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "wwfy" extends = "struts-default" > < global-results > < result name = "test" >/index.jsp</ result > </ global-results > </ package > </ struts > |