Struts2 常用攔截器

轉自:http://neil-jh.javaeye.com/blog/197882

Struts2 XWork )提供的攔截器的功能說明:

 

攔截器

名字

說明

Alias Interceptor

alias

在不同請求之間將請求參數在不同名字件轉換,請求內容不變

Chaining Interceptor

chain

讓前一個 Action 的屬性可以被後一個 Action 訪問,現在和 chain 類型的 result <result type=”chain”> )結合使用。

Checkbox Interceptor

checkbox

添加了 checkbox 自動處理代碼,將沒有選中的 checkbox 的內容設定爲 false ,而 html 默認情況下不提交沒有選中的 checkbox

Cookies Interceptor

cookies

使用配置的 name,value 來是指 cookies

Conversion Error Interceptor

conversionError

將錯誤從 ActionContext 中添加到 Action 的屬性字段中。

Create Session Interceptor

createSession

自動的創建 HttpSession ,用來爲需要使用到 HttpSession 的攔截器服務。

Debugging Interceptor

debugging

提供不同的調試用的頁面來展現內部的數據狀況。

Execute and Wait Interceptor

execAndWait

在後臺執行 Action ,同時將用戶帶到一箇中間的等待頁面。

Exception Interceptor

exception

將異常定位到一個畫面

File Upload Interceptor

fileUpload

提供文件上傳功能

I18n Interceptor

i18n

記錄用戶選擇的 locale

Logger Interceptor

logger

輸出 Action 的名字

Message Store Interceptor

store

存儲或者訪問實現 ValidationAware 接口的 Action 類出現的消息,錯誤,字段錯誤等。

Model Driven Interceptor

model-driven

如果一個類實現了 ModelDriven ,將 getModel 得到的結果放在 Value Stack 中。

Scoped Model Driven

scoped-model-driven

如果一個 Action 實現了 ScopedModelDriven ,則這個攔截器會從相應的 Scope 中取出 model 調用 Action setModel 方法將其放入 Action 內部。

Parameters Interceptor

params

將請求中的參數設置到 Action 中去。

Prepare Interceptor

prepare

如果 Acton 實現了 Preparable ,則該攔截器調用 Action 類的 prepare 方法。

Scope Interceptor

scope

Action 狀態存入 session application 的簡單方法。

Servlet Config Interceptor

servletConfig

提供訪問 HttpServletRequest HttpServletResponse 的方法,以 Map 的方式訪問。

Static Parameters Interceptor

staticParams

struts.xml 文件中將 <action> 中的 <param> 中的內容設置到對應的 Action 中。

Roles Interceptor

roles

確定用戶是否具有 JAAS 指定的 Role ,否則不予執行。

Timer Interceptor

timer

輸出 Action 執行的時間

Token Interceptor

token

通過 Token 來避免雙擊

Token Session Interceptor

tokenSession

Token Interceptor 一樣,不過雙擊的時候把請求的數據存儲在 Session

Validation Interceptor

validation

使用 action-validation.xml 文件中定義的內容校驗提交的數據。

Workflow Interceptor

workflow

調用 Action validate 方法,一旦有錯誤返回,重新定位到 INPUT 畫面

Parameter Filter Interceptor

N/A

從參數列表中刪除不必要的參數

Profiling Interceptor

profiling

通過參數激活 profile

 

另外 AbstractInterceptor 提供了一個簡單的 Interceptor 的實現,這個實現爲:

public abstract class AbstractInterceptor implements Interceptor {

 

     public void init() {

    }

   

    public void destroy() {

    }

 

 

    public abstract String intercept(ActionInvocation invocation) throws Exception;

}

在不需要編寫 init destroy 方法的時候,只需要從 AbstractInterceptor 繼承而來,實現 intercept 方法即可。

 

我們嘗試編寫一個 Session 過濾用的攔截器,該攔截器查看用戶 Session 中是否存在特定的屬性( LOGIN 屬性)如果不存在,中止後續操作定位到 LOGIN ,否則執行原定操作,代碼爲:

public class CheckLoginInterceptor extends AbstractInterceptor {

    public static final String LOGIN_KEY = "LOGIN";

    public static final String LOGIN_PAGE = "global.login";

 

     public String intercept(ActionInvocation actionInvocation) throws Exception {

 

        System.out.println("begin check login interceptor!");

        // LoginAction 不做該項攔截

        Object action = actionInvocation.getAction();

        if (action instanceof LoginAction) {

            System.out.println("exit check login, because this is login action.");

            return actionInvocation.invoke();

        }

 

        // 確認 Session 中是否存在 LOGIN

        Map session = actionInvocation.getInvocationContext().getSession();

        String login = (String) session.get(LOGIN_KEY);

        if (login != null && login.length() > 0) {

            // 存在的情況下進行後續操作。

            System.out.println("already login!");

            return actionInvocation.invoke();

        } else {

             // 否則終止後續操作,返回 LOGIN

            System.out.println("no login, forward login page!");

            return LOGIN_PAGE;

        }

    }

}

 

註冊攔截器

<interceptors>

            <interceptor

name="login" 

class="com.jpleasure.teamware.util.CheckLoginInterceptor"/>

            <interceptor-stack name="teamwareStack">

                <interceptor-ref name="login"/>

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

</interceptors>

 

將上述攔截器設定爲默認攔截器:

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

這樣在後續同一個 package 內部的所有 Action 執行之前都會被 login 攔截。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章