Struts 2 攔截器,struts攔截器

來源:互聯網
上載者:User

Struts 2 攔截器,struts攔截器
什麼是Struts 2 攔截器

   

 攔截器就是當使用者請求後台Action類時在Action的Excute()方法執行前和Result返回魔板試圖之後(將頁面(資料)發送給瀏覽器渲染之前)所需要的一些通用操作存放在攔截器中對資料進行攔截!

簡單來說就是對請求和響應資訊進行過濾,可以看做是Java EE中的過濾器,但是需要注意的是攔截器只能對Action類的請求進行攔截若直接請求jsp檔案、Css樣式等靜態檔案攔截器是無法進行過濾的!

為什麼要使用攔截器
   

任何優秀的MVC架構都會提供一些通用的操作,如請求資料的封裝、類型轉換、資料校正、解析上傳的表單、防止表單的多次提交等。而早期的MVC架構中,這些通用的操作都寫死在核心控制器中,然而並不是所用的請求都需要這些操作的實現,於是就導致了架構的靈活性差、可擴充性低等問題。

Struts2將它的核心功能放到攔截器而不是集中在核心控制器中實現,把大部分控制器需要完成的工作按功能分開定義,每個攔截器只完成一個功能,而完成這些功能的攔截器可以自由選擇,靈活組合,需要哪些功能只需要在設定檔中進行添加即可,從而提高了架構的靈活性。

攔截器的工作原理
   

Struts2攔截器圍繞Action和Result的執行而執行。其實現原理和Servlet Filter差不多,以鏈式執行,對真正要執行的方法(execute())進行攔截。首先執行Action配置的攔截器,在Action和Result執行之後,攔截器在一次執行(與先前的執行順序相反),在此以鏈式的執行過程中,任何一個攔截器都可以直接返回,從而終止餘下的攔截器、Action及Result的執行。

當ActionInvocation的invoke()方法被調用時,開始執行Action配置的第一個攔截器,攔截器作出相應的處理後會在此調用ActionInvocation的invoke()方法,ActionInvocation對象負責跟蹤執行過程的狀態,並且把控制權交給合適的攔截器。ActionInvocation通過調用攔截器的intercept()方法將控制轉交給攔截器。因此,攔截器的執行過程可以看做是一個遞迴的過程,後續攔截器繼續執行,直到最後一個攔截器,invoke()方法會執行Action的execut()方法。

先面以一張圖來描述攔截器的執行過程

<!--攔截器棧(可以存放一堆攔截器,在調用時只需調用defaultStack就可以調用其中的所有攔截器)--> <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/><!--用於捕獲異常,並根據類型將異常映射到使用者自訂的錯誤頁面--> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/><!--將源於Servlet API的各種對象注入 Action當中--> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/><!--將檔案和中繼資料從多重請求轉換為常規的請求資料,以便能將他們設定在隊形的Action屬性中--> <interceptor-ref name="checkbox"/> <interceptor-ref name="datetime"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="staticParams"/><!--將在設定檔中通過action的子項目param設定的參數設定到對應的Action屬性中--> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"/><!--將請求中的資料設定到Action中的屬性上--> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"><!--用於資料校正--> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"><!--用於資料校正錯誤時終止執行流程--> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="debugging"/> <interceptor-ref name="deprecation"/> </interceptor-stack>

自訂攔截器
  

介紹了那麼多的原理,現在我們來自己編寫一個攔截器來體驗一下吧。下面來說一下我們這個例子的功能。首先當使用者訪問我們的網站時使用攔截器判斷其是否已經登入,如果沒有登入則跳轉到登入頁面進行登入:

如果是已經登入的使用者則直接跳轉到歡迎介面,:

  

關於頁面的html和登入的驗證代碼這裡就不在進行展示了,我們主要看一下關於攔截器的代碼,首先我們要定義一個攔截器類(該類實現com.opensymphony.xwork2.interceptor.Interceptor 介面)。其代碼如下:

package cn.wz.logindemo;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class Myinterceptor implements Interceptor {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * 實現Interceptor介面的intercept()方法,該方法進行攔截操作,
     */
    public String intercept(ActionInvocation invoc) throws Exception {
        ActionContext context = invoc.getInvocationContext();//擷取Action內容物件
        Map<String, Object> map = context.getSession();//通過Action上下文擷取Session對象的集合(Session實質上就是封裝了一個map集合,通過getSession()方法來以解耦的方式擷取Session)
        boolean falg = map.containsKey("userName");//判斷使用者是否已經登入
        if (falg) {
            //如果已經登入則將控制權轉交給其它的攔截器或Action
                 return     invoc.invoke();
       
        }
        //如果沒有登入則返回“input”不再執行其他攔截器或Action直接跳轉到登入頁面
    return "input";
    }
    /**
     * 實現Interceptor介面的destory()方法,該方法只在該攔截器銷毀時調用一次
     */
    public void destroy() {
       
    }
    /**
     * 實現Interceptor介面的init方法,該方法只在該攔截器初始化時調用一次
     */
    public void init() {
       
    }


}

然後在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.