|
如何使用struts2攔截器,或者自定義攔截器。特別注意,在使用攔截器的時(shí)候,在Action里面必須最后一定要引用struts2自帶的攔截器缺省堆棧defaultStack,如下(這里我是引用了struts2自帶的checkbox攔截器): 也可以改為對(duì)全局Action設(shè)置自己需要的攔截器,如下: 你的攔截器可以正常工作了?。OHO 以下是參考資料
攔截器的工作原理如上圖,每一個(gè)Action請(qǐng)求都包裝在一系列的攔截器的內(nèi)部。攔截器可以在Action執(zhí)行直線做相似的操作也可以在Action執(zhí)行直后做回收操作。
每一個(gè)Action既可以將操作轉(zhuǎn)交給下面的攔截器,Action也可以直接退出操作返回客戶既定的畫面。
如何自定義一個(gè)攔截器? 自定義一個(gè)攔截器需要三步: 1 自定義一個(gè)實(shí)現(xiàn)Interceptor接口(或者繼承自AbstractInterceptor)的類。 2 在strutx.xml中注冊(cè)上一步中定義的攔截器。 3 在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認(rèn)的攔截器,這樣在不加特殊聲明的情況下所有的Action都被這個(gè)攔截器攔截。
Interceptor接口聲明了三個(gè)方法:
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception; }
Init方法在攔截器類被創(chuàng)建之后,在對(duì)Action鏡像攔截之前調(diào)用,相當(dāng)于一個(gè)post-constructor方法,使用這個(gè)方法可以給攔截器類做必要的初始話操作。
Destroy方法在攔截器被垃圾回收之前調(diào)用,用來回收init方法初始化的資源。
Intercept是攔截器的主要攔截方法,如果需要調(diào)用后續(xù)的Action或者攔截器,只需要在該方法中調(diào)用invocation.invoke()方法即可,在該方法調(diào)用的前后可以插入Action調(diào)用前后攔截器需要做的方法。如果不需要調(diào)用后續(xù)的方法,則返回一個(gè)String類型的對(duì)象即可,例如Action.SUCCESS。 另外AbstractInterceptor提供了一個(gè)簡(jiǎn)單的Interceptor的實(shí)現(xiàn),這個(gè)實(shí)現(xiàn)為: public abstract class AbstractInterceptor implements Interceptor {
public void init() { }
public void destroy() { }
public abstract String intercept(ActionInvocation invocation) throws Exception; } 在不需要編寫init和destroy方法的時(shí)候,只需要從AbstractInterceptor繼承而來,實(shí)現(xiàn)intercept方法即可。
我們嘗試編寫一個(gè)Session過濾用的攔截器,該攔截器查看用戶Session中是否存在特定的屬性(LOGIN屬性)如果不存在,中止后續(xù)操作定位到LOGIN,否則執(zhí)行原定操作,代碼為: 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!"); // 對(duì)LoginAction不做該項(xiàng)攔截 Object action = actionInvocation.getAction(); if (action instanceof LoginAction) { System.out.println("exit check login, because this is login action."); return actionInvocation.invoke(); }
// 確認(rèn)Session中是否存在LOGIN Map session = actionInvocation.getInvocationContext().getSession(); String login = (String) session.get(LOGIN_KEY); if (login != null && login.length() > 0) { // 存在的情況下進(jìn)行后續(xù)操作。 System.out.println("already login!"); return actionInvocation.invoke(); } else { // 否則終止后續(xù)操作,返回LOGIN System.out.println("no login, forward login page!"); return LOGIN_PAGE; } } }
注冊(cè)攔截器 <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>
將上述攔截器設(shè)定為默認(rèn)攔截器: <default-interceptor-ref name="teamwareStack"/> 這樣在后續(xù)同一個(gè)package內(nèi)部的所有Action執(zhí)行之前都會(huì)被login攔截。
Struts2(XWork)提供的攔截器的功能說明:
注冊(cè)并引用Interceptor <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> </interceptors>
<action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
可以將多個(gè)攔截器合并在一起作為一個(gè)堆棧調(diào)用,當(dāng)一個(gè)攔截器堆棧被附加到一個(gè)Action的時(shí)候,要想Action執(zhí)行,必須執(zhí)行攔截器堆棧中的每一個(gè)攔截器。 <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> </interceptor-stack> </interceptors>
<action name="login" class="tutuorial.Login"> <interceptor-ref name="myStack"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
上述說明的攔截器在默認(rèn)的Struts2應(yīng)用中,根據(jù)慣例配置了若干個(gè)攔截器堆棧,詳細(xì)情參看struts-default.xml 其中有一個(gè)攔截器堆棧比較特殊,他會(huì)應(yīng)用在默認(rèn)的每一個(gè)Action上。 <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo"..*</param> </interceptor-ref> <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-stack>
每一個(gè)攔截器都可以配置參數(shù),有兩種方式配置參數(shù),一是針對(duì)每一個(gè)攔截器定義參數(shù),二是針對(duì)一個(gè)攔截器堆棧統(tǒng)一定義所有的參數(shù),例如: <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>
每一個(gè)攔截器都有兩個(gè)默認(rèn)的參數(shù): excludeMethods - 過濾掉不使用攔截器的方法和 includeMethods – 使用攔截器的方法。
需要說明的幾點(diǎn): 1 攔截器執(zhí)行的順序按照定義的順序執(zhí)行,例如: <interceptor-stack name="xaStack"> <interceptor-ref name="thisWillRunFirstInterceptor"/> <interceptor-ref name="thisWillRunNextInterceptor"/> <interceptor-ref name="followedByThisInterceptor"/> <interceptor-ref name="thisWillRunLastInterceptor"/> </interceptor-stack> 的執(zhí)行順序?yàn)椋?/font> thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor
2 使用默認(rèn)攔截器配置每個(gè)Action都需要的攔截器堆棧,例如: <action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/>
<result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action> 可以按照如下的方式定義: <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> </interceptor-stack> </interceptors>
<default-interceptor-ref name="myStack"/>
<action name="login" class="tutorial.Login"> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action>
3 如何訪問HttpServletRequest,HttpServletResponse或者HttpSession 有兩種方法可以達(dá)到效果,使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 或者實(shí)現(xiàn)相應(yīng)的接口: HttpSession SessionAware HttpServletRequest ServletRequestAware HttpServletResponse ServletResponseAware |
|
|