struts.xml配置詳解

struts.xml配置詳解

struts.xml是我們在開發(fā)中利用率最高的文件,也是Struts2中最重要的配置文件。

一下分別介紹一下幾個(gè)struts.xml中常用到的標(biāo)簽

1、<include>

利用include標(biāo)簽,可以將一個(gè)struts.xml配置文件分割成多個(gè)配置文件,然后在struts.xml中使用<include>標(biāo)簽引入其他配置文件。

比如一個(gè)網(wǎng)上購物程序,可以把用戶配置、商品配置、訂單配置分別放在3個(gè)配置文件user.xml、goods.xml和order.xml中,然后在struts.xml中將這3個(gè)配置文件引入:

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>
    <include file="user.xml"/>
    <include file="goods.xml"/>
    <include file="order.xml"/>
</struts>

user.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>
    <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配置文件的介紹中,我們曾經(jīng)提到所有在struts.properties文件中定義的屬性,都可以配置在struts.xml文件中。而在struts.xml中,是通過<constant>標(biāo)簽來進(jìn)行配置的:

<?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"/>

<constant name="struts.i18n.encoding" value="GB2312"/>

</struts>

3、<package>

1、包屬性介紹

在Struts2框架中是通過包來管理action、result、interceptor、interceptor-stack等配置信息的。包屬性如下:

屬性

是否必需

描述

name 是 包名,作為其它包應(yīng)用本包的標(biāo)記
extends 否 設(shè)置本包繼承其它包
namespace 否 設(shè)置包的命名空間
abstact 否 設(shè)置為抽象包

2、extends屬性的詳解

當(dāng)一個(gè)包通過配置extends屬性繼承了另一個(gè)包的時(shí)候,該包將會繼承父包中所有的配置,包括action、result、interceptor等。
由于包信息的獲取是按照配置文件的先后順序進(jìn)行的,所以父包必須在子包之前被定義。
通常我們配置struts.xml的時(shí)候,都繼承一個(gè)名為“struts-default.xml”的包,這是struts2中內(nèi)置的包。

3、namespace的詳解

namespace主要是針對大型項(xiàng)目中Action的管理,更重要的是解決Action重名問題,因?yàn)椴辉谕粋€(gè)命名空間的Action可以使用相同的Action名的。

1)如果使用命名空間則URL將改變

比如我們有一下配置文件

<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

假如為這個(gè)包指定了命名空間

<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)默認(rèn)命名空間

Struts2中如果沒有為某個(gè)包指定命名空間,該包使用默認(rèn)的命名空間,默認(rèn)的命名空間總是""。

3)指定根命名空間

當(dāng)設(shè)置了命名空間為“/”,即指定了包的命名空間為根命名空間時(shí),此時(shí)所有根路徑下的Action請求都會去這個(gè)包中查找對應(yīng)的資源信息。

假若前例中路徑為http://localhost:8080/login.action則所有http://localhost:8080/*.action都會到設(shè)置為根命名空間的包中尋找資源。

4、<action>與<result>

1、<action>屬性介紹

屬性名稱

是否必須

功能描述

name 是 請求的Action名稱
class 否 Action處理類對應(yīng)具體路徑
method 否 指定Action中的方法名
converter 否 指定Action使用的類型轉(zhuǎn)換器
如果沒有指定method則默認(rèn)執(zhí)行Action中的execute方法。

2、<result>屬性介紹

屬性名稱

是否必須

功能描述

name 否 對應(yīng)Action返回邏輯視圖名稱,默認(rèn)為success
type 否 返回結(jié)果類型,默認(rèn)為dispatcher

3、通配符的使用

隨著result的增加,struts.xml文件也會隨之變得越來越復(fù)雜。那么就可以使用通配符來簡化配置:

例如下面這個(gè)案例:

Action為Test.java

public class Test {
   public String test1(){
       return "result1";
   }
    
   public String test2(){
       return "result2";
   }
    
   public String test3(){
       return "result3";
   }
}

struts.xml中配置為

<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方法的另一種實(shí)現(xiàn)方式

在Struts2中如果要訪問Action中的指定方法,還可以通過改變URL請求來實(shí)現(xiàn),將原本的“Action名稱.action”改為“Action名稱!方法名稱.action”在struts.xml中就不需要指定方法名了。

5、<exception-mapping>與<global-exception-mapping>

這兩個(gè)標(biāo)簽都是用來配置發(fā)生異常時(shí)對應(yīng)的視圖信息的,只不過一個(gè)是Action范圍的,一個(gè)是包范圍的,當(dāng)同一類型異常在兩個(gè)范圍都被配置時(shí),Action范圍的優(yōu)先級要高于包范圍的優(yōu)先級.這兩個(gè)標(biāo)簽包含的屬性也是一樣的:

屬性名稱

是否必須

功能描述

name 否 用來表示該異常配置信息
result 是 指定發(fā)生異常時(shí)顯示的視圖信息,這里要配置為邏輯視圖
exception 是 指定異常類型

兩個(gè)標(biā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>
    <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>

當(dāng)我們在配置Action的時(shí)候,如果沒有為某個(gè)Action指定具體的class值時(shí),系統(tǒng)將自動引用<default-class-ref>標(biāo)簽中所指定的類。在Struts2框架中,系統(tǒng)默認(rèn)的class為ActionSupport,該配置我們可以在xwork的核心包下的xwork-default.xml文件中找到。

有特殊需要時(shí),可以手動指定默認(rèn)的class

package wwfy.action;
 
public class DefaultClassRef {
    public void execute(){
        System.out.println("默認(rèn)class開始執(zhí)行……");
    }
}

在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>
    <package name="wwfy" extends="struts-default">
        <!-- 指定默認(rèn)class為Test -->
        <default-class-ref class="wwfy.action.DefaultClassRef"/>
        <action name="test1">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

7、<default-action-ref>

如果在請求一個(gè)沒有定義過的Action資源時(shí),系統(tǒng)就會拋出404錯(cuò)誤。這種錯(cuò)誤不可避免,但這樣的頁面并不友好。我們可以使用<default-action-ref>來指定一個(gè)默認(rèn)的Action,如果系統(tǒng)沒有找到指定的Action,就會指定來調(diào)用這個(gè)默認(rèn)的Action。

<?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>
    <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>

該標(biāo)簽用來設(shè)置整個(gè)包范圍內(nèi)所有Action所要應(yīng)用的默認(rèn)攔截器信息。事實(shí)上我們的包繼承了struts-default包以后,使用的是Struts的默認(rèn)設(shè)置。我們可以在struts-default.xml中找到相關(guān)配置:

<default-interceptor-ref name="defaultStack"/>

在實(shí)際開發(fā)過程中,如果我們有特殊的需求是可以改變默認(rèn)攔截器配置的。當(dāng)時(shí)一旦更改這個(gè)配置,“defaultStack”將不再被引用,需要手動最加。

9、<interceptors>

通過該標(biāo)簽可以向Struts2框架中注冊攔截器或者攔截器棧,一般多用于自定義攔截器或攔截器棧的注冊。該標(biāo)簽使用方法如下:

<interceptors>
    <interceptor name="攔截器名" class="攔截器類"/>
    <interceptor-stack name="攔截器棧名">
        <interceptor-ref name="攔截器名">
    </interceptor-stack>
</interceptors>

10、<interceptor-ref>

通過該標(biāo)簽可以為其所在的Action添加攔截器功能。當(dāng)為某個(gè)Action單獨(dú)添加攔截器功能后,<default-interceptor-ref>中所指定的攔截器將不再對這個(gè)Action起作用。

11、<global-results>

該標(biāo)簽用于設(shè)置包范圍內(nèi)的全局結(jié)果集。在多個(gè)Action返回相同邏輯視圖的情況下,可以通過<global-results>標(biāo)簽統(tǒng)一配置這些物理視圖所對應(yīng)的邏輯視圖。

<?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>
    <package name="wwfy" extends="struts-default">
        <global-results>
            <result name="test">/index.jsp</result>
        </global-results>
    </package>
</struts>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容