struts.xml配置詳解

來源:互聯網
上載者:User

標籤:

以下分別介紹一下幾個struts.xml中常用到的標籤

1、<include>

利用include標籤,可以將一個struts.xml設定檔分割成多個設定檔,然後在struts.xml中使用<include>標籤引入其他設定檔。

比如一個網上購物程式,可以把使用者配置、商品配置、訂單配置分別放在3個設定檔user.xml、goods.xml和order.xml中,然後在struts.xml中將這3個設定檔引入:

struts.xml:

代碼

 

user.xml:

代碼

 

2、<constant>

在之前提到struts.properties設定檔的介紹中,我們曾經提到所有在struts.properties檔案中定義的屬性,都可以配置在struts.xml檔案中。而在struts.xml中,是通過<constant>標籤來進行配置的:

 

代碼
<?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"/>
<!--設定編碼形式為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將改變

比如我們有一下設定檔

<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

假如為這個包指定了命名空間

<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

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方法的另一種實現方式

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

 

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

這兩個標籤都是用來配置發生異常時對應的視圖資訊的,只不過一個是Action範圍的,一個是包範圍的,當同一類型異常在兩個範圍都被配置時,Action範圍的優先順序要高於包範圍的優先順序.這兩個標籤包含的屬性也是一樣的:

屬性名稱

是否必須

功能描述

name 用來表示該異常配置資訊
result 指定發生異常時顯示的視圖資訊,這裡要配置為邏輯視圖
exception 指定異常類型

 

兩個標籤的範例程式碼為:

 

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

當我們在配置Action的時候,如果沒有為某個Action指定具體的class值時,系統將自動引用<default-class-ref>標籤中所指定的類。在Struts2架構中,系統預設的class為ActionSupport,該配置我們可以在xwork的核心包下的xwork-default.xml檔案中找到。

有特殊需要時,可以手動指定預設的class

package wwfy.action;

public class DefaultClassRef {
public void execute(){
System.out.println("預設class開始執行……");
}
}

 

 

在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">
<!-- 指定預設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。

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

該標籤用來設定整個包範圍內所有Action所要應用的預設攔截器資訊。事實上我們的包繼承了struts-default包以後,使用的是Struts的預設設定。我們可以在struts-default.xml中找到相關配置:

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

在實際開發過程中,如果我們有特殊的需求是可以改變預設攔截器配置的。當時一旦更改這個配置,“defaultStack”將不再被引用,需要手動最加。

 

9、<interceptors>

通過該標籤可以向Struts2架構中註冊攔截器或者攔截器棧,一般多用於自訂攔截器或攔截器棧的註冊。該標籤使用方法如下:

<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>標籤統一配置這些物理視圖所對應的邏輯視圖。

代碼
<?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>
參考連結:http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html

struts.xml配置詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.