structs2攔截器簡單實現

1、理解Struts2攔截器html

1. Struts2攔截器是在訪問某個Action或Action的某個方法,字段以前或以後實施攔截,而且Struts2攔截器是可插拔的,攔截器是AOP的一種實現.spring

2. 攔截器棧(Interceptor Stack)。Struts2攔截器棧就是將攔截器按必定的順序聯結成一條鏈。在訪問被攔截的方法或字段時,Struts2攔截器鏈中的攔截器就會按其以前定義的順序被調用。apache

2、實現Struts2攔截器原理cookie

Struts2攔截器的實現原理相對簡單,當請求struts2的action時,Struts 2會查找配置文件,並根據其配置實例化相對的 攔截器對象,而後串成一個列表,最後一個一個地調用列表中的攔截器session

3、定義Struts2攔截器。app

Struts2規定用戶自定義攔截器必須實現com.opensymphony.xwork2.interceptor.Interceptor接口。該接口聲明瞭3個方法,jsp

void init();
void destroy();
String intercept(ActionInvocation invocation) throws Exception;
 ide

其中,init和destroy方法會在程序開始和結束時各執行一遍,無論使用了該攔截器與否,只要在struts.xml中聲明瞭該Struts2攔截器就會被執行。
intercept方法就是攔截的主體了,每次攔截器生效時都會執行其中的邏輯。spa

 

不過,struts中又提供了幾個抽象類來簡化這一步驟。debug

 

public abstract class AbstractInterceptor implements Interceptor;
public abstract class MethodFilterInterceptor extends AbstractInterceptor;

 

都是模板方法實現的。

其中AbstractInterceptor提供了init()和destroy()的空實現,使用時只須要覆蓋intercept()方法;

而MethodFilterInterceptor則提供了includeMethods和excludeMethods兩個屬性,用來過濾執行該過濾器的action的方法。能夠經過param來加入或者排除須要過濾的方法。

通常來講,攔截器的寫法都差很少。看下面的示例:

 

package interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
public void destroy() {
// TODO Auto-generated method stub
}
public void init() {
// TODO Auto-generated method stub
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Action執行前插入 代碼");
//執行目標方法 (調用下一個攔截器, 或執行Action)
final String res = invocation.invoke();
System.out.println("Action執行後插入 代碼");
return res;
}
}

 

4、配置Struts2攔截器

Struts2攔截器須要在struts.xml中聲明,以下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>
<constant name="struts.objectFactory" value="spring" />

 

<package name="default" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="MyInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="loginAction" class="loginAction">
<result name="fail">/index.jsp </result>
<result name="success">/success.jsp</result>
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
</package>
</struts>

 

 

 

攔截器

名字

說明

Alias Interceptor

alias

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

Chaining Interceptor

chain

讓前一個Action的屬性能夠被後一個Action訪問,如今和chain類型的result )結合使用。

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調用ActionsetModel方法將其放入Action內部。

Parameters Interceptor

params

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

Prepare Interceptor

prepare

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

Scope Interceptor

scope

Action狀態存入sessionapplication的簡單方法。

Servlet Config Interceptor

servletConfig

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

Static Parameters Interceptor

staticParams

struts.xml文件中將 中的 中的內容設置到對應的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

調用Actionvalidate方法,一旦有錯誤返回,從新定位到INPUT畫面

Parameter Filter Interceptor

N/A

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

Profiling Interceptor

profiling

經過參數激活profile

相關文章
相關標籤/搜索