Struts2攔截器淺解,struts2攔截

來源:互聯網
上載者:User

Struts2攔截器淺解,struts2攔截

  最近在學習struts的攔截器,現在來總結一下。

1、攔截器是什嗎?

  攔截器相當於過濾器:就是將不想要的去掉,想要的留下。攔截器抽象出一部分代碼可以用來完善原來的action。同時可以減輕代碼冗餘,提高重用率。通俗地講就是一張網,過濾掉不需要的沙子,留下水。

2、攔截器的作用:

  攔截器可以構成特定的功能。比如許可權認證、日誌記錄和登陸判斷。

3、攔截器的原理:

  

其每一個Action請求都在攔截器中,每一個action可以將操作轉交給下面的攔截器,也可以直接退出到介面上。

4、定義攔截器:

  (1)自訂一個實現Interceptor介面(不過我初學者一般直接實現架構中的Interceptor)

  (2)在struts.xml中註冊定義的攔截器

  (3)可以需要的action中引用攔截器

Interceptor介面聲明了三個方法

  

1 public interface Interceptor extends Serializable {2  3     void destroy();4  5     void init();6  7     String intercept(ActionInvocation invocation) throws Exception;8 }

  Init方法是在action作用之前調用,就是開始給爛機器的初始化操作。

  Destory方法在攔截器被記憶體回收之前調用,用來回收init方法初始化的資源。

  interceptor方法是攔截器的主要操作。如果需要調用後續的Action或者攔截器,只需要在該方法中調用invocation.invoke()方法即可,在該方法調用的前後可以插入Action調用前後攔截器需要做的方法。

  現在對使用者登入進行攔截,代碼如下:

public String intercept(ActionInvocation invocation) throws Exception {                System.out.println("在action執行之前");        ActionContext actionContext=invocation.getInvocationContext();        Map<String,Object> session=actionContext.getSession();                Object currentUser=session.get("currentUser");        String result=null;        if(currentUser!=null){            result=invocation.invoke();                    }else{                        HttpServletRequest request=(HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);            request.setAttribute("error", "請先登入");            result="error";        }        System.out.println("result+"+result);        System.out.println("在action執行之後");        return result;    }

  註冊攔截器:

<interceptors>             <interceptor name="myInterceptor"             class="com.fangchao.interceptor.MyInterceptor"></interceptor>             <interceptor name="loginInterceptor"             class="com.fangchao.interceptor.LoginInterceptor"></interceptor>                          <interceptor-stack name="myStack">                 <interceptor-ref name="loginInterceptor"></interceptor-ref>                 <interceptor-ref name="defaultStack"></interceptor-ref>                 </interceptor-stack>         </interceptors>

  上述代碼中的interceptor-stack是個攔截器棧。到目前為止,就是在下面引用時,比較方便。一般來講,每個action都會使用defaultStack。

  攔截器參數:

 配置參數:

  excludeMethods:過濾掉不使用攔截器的方法

  includeMethods:使用攔截器的方法。

有兩種配置方式:

  【1】

<interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param></interceptor-ref><interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param></interceptor-ref>或者<interceptor-ref name="defaultStack">    <param name="validation.excludeMethods">myValidationExcludeMethod</param>    <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param></interceptor-ref>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.