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:
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< include
file = "user.xml" />
|
08 |
< include
file = "goods.xml" />
|
09 |
< include
file = "order.xml" />
|
user.xml:
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< package
name = "wwfy"
extends = "struts-default" >
|
08 |
< action
name = "login"
class = "wwfy.user.LoginAction" >
|
11 |
< action
name = "logout"
class = "wwfy.user.LogoutAction" >
|
2、<constant>
在之前提到struts.properties配置文件的介紹中,我們曾經提到所有在struts.properties文件中定義的屬性,都可以配置在struts.xml文件中。而在struts.xml中,是通過<constant>標籤來進行配置的:
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
08 |
< constant
name = "struts.devMode"
value = "true" />
|
10 |
< constant
name = "struts.i18n.encoding"
value = "GB2312" />
|
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 |
< package
name = "wwfy"
extends = "struts-default" >
|
2 |
< action
name = "login"
class = "wwfy.action.LoginAction" >
|
3 |
< result >/success.jsp</ result >
|
則此配置下的Action的URL爲http://localhost:8080/login.action
假如爲這個包指定了命名空間
1 |
< package
name = "wwfy"
extends = "struts-default"
namespace = "/user" >
|
2 |
< action
name = "login"
class = "wwfy.action.LoginAction" >
|
3 |
< result >/success.jsp</ result >
|
則此配置下的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
02 |
public
String test1(){ |
06 |
public
String test2(){ |
10 |
public
String test3(){ |
struts.xml中配置爲
1 |
< package
name = "wwfy"
extends = "struts-default" >
|
2 |
< action
name = "test*"
class = "wwfy.action.test{1}" >
|
3 |
< result
name = "result{1}" >/result{1}.jsp</ result >
|
4、訪問Action方法的另一種實現方式
在Struts2中如果要訪問Action中的指定方法,還可以通過改變URL請求來實現,將原本的“Action名稱.action”改爲“Action名稱!方法名稱.action”在struts.xml中就不需要指定方法名了。
5、<exception-mapping>與<global-exception-mapping>
這兩個標籤都是用來配置發生異常時對應的視圖信息的,只不過一個是Action範圍的,一個是包範圍的,當同一類型異常在兩個範圍都被配置時,Action範圍的優先級要高於包範圍的優先級.這兩個標籤包含的屬性也是一樣的:
屬性名稱
|
是否必須
|
功能描述
|
name |
否 |
用來表示該異常配置信息 |
result |
是 |
指定發生異常時顯示的視圖信息,這裏要配置爲邏輯視圖 |
exception |
是 |
指定異常類型 |
兩個標籤的示例代碼爲:
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< package
name = "default"
extends = "struts-default" >
|
08 |
< global-exception-mappings >
|
09 |
< exception-mapping
result = "邏輯視圖"
exception = "異常類型" />
|
10 |
</ global-exception-mappings >
|
11 |
< action
name = "Action名稱" >
|
12 |
< exception-mapping
result = "邏輯視圖"
exception = "異常類型" />
|
6、<default-class-ref>
當我們在配置Action的時候,如果沒有爲某個Action指定具體的class值時,系統將自動引用<default-class-ref>標籤中所指定的類。在Struts2框架中,系統默認的class爲ActionSupport,該配置我們可以在xwork的核心包下的xwork-default.xml文件中找到。
有特殊需要時,可以手動指定默認的class
3 |
public
class DefaultClassRef {
|
5 |
System.out.println( "默認class開始執行……" );
|
在struts.xml中配置
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< package
name = "wwfy"
extends = "struts-default" >
|
09 |
< default-class-ref
class = "wwfy.action.DefaultClassRef" />
|
11 |
< result >/index.jsp</ result >
|
7、<default-action-ref>
如果在請求一個沒有定義過的Action資源時,系統就會拋出404錯誤。這種錯誤不可避免,但這樣的頁面並不友好。我們可以使用<default-action-ref>來指定一個默認的Action,如果系統沒有找到指定的Action,就會指定來調用這個默認的Action。
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< package
name = "wwfy"
extends = "struts-default" >
|
09 |
< default-action-ref
name = "acctionError" ></ default-action-ref >
|
10 |
< action
name = "acctionError" >
|
11 |
< result >/jsp/actionError.jsp</ result >
|
8、<default-interceptor-ref>
該標籤用來設置整個包範圍內所有Action所要應用的默認攔截器信息。事實上我們的包繼承了struts-default包以後,使用的是Struts的默認設置。我們可以在struts-default.xml中找到相關配置:
1 |
< default-interceptor-ref
name = "defaultStack" /> |
在實際開發過程中,如果我們有特殊的需求是可以改變默認攔截器配置的。當時一旦更改這個配置,“defaultStack”將不再被引用,需要手動最加。
9、<interceptors>
通過該標籤可以向Struts2框架中註冊攔截器或者攔截器棧,一般多用於自定義攔截器或攔截器棧的註冊。該標籤使用方法如下:
2 |
< interceptor
name = "攔截器名"
class = "攔截器類" />
|
3 |
< interceptor-stack
name = "攔截器棧名" >
|
4 |
< interceptor-ref
name = "攔截器名" >
|
10、<interceptor-ref>
通過該標籤可以爲其所在的Action添加攔截器功能。當爲某個Action單獨添加攔截器功能後,<default-interceptor-ref>中所指定的攔截器將不再對這個Action起作用。
11、<global-results>
該標籤用於設置包範圍內的全局結果集。在多個Action返回相同邏輯視圖的情況下,可以通過<global-results>標籤統一配置這些物理視圖所對應的邏輯視圖。
01 |
<? xml
version = "1.0"
encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE struts PUBLIC
|
03 |
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
07 |
< package
name = "wwfy"
extends = "struts-default" >
|
09 |
< result
name = "test" >/index.jsp</ result >
|