struts2中使用自定義攔截器
雖然Strtus2框架提供了許多攔截器,這些內置的攔截器實現了Struts2的大部分功能,因此大部分web應用程序的通用功能都可以通過直接使用這些攔截器來完成,但還有一些系統邏輯相關的通用功能,則可以通過自定義攔截器來實現。值得稱道的是,Struts2的攔截器系統是如此的簡單,易用。
一:實現攔截器
如果程序員要開發自己的攔截器類,應該實現com.opensymphony.xwork2.interceptor接口,該接口代碼如下(struts2源碼):
package com.opensymphony.xwork2.interceptor; import com.opensymphony.xwork2.ActionInvocation; import java.io.Serializable; public abstract interface Interceptor extends Serializable{ public abstract void destroy(); public abstract void init(); public abstract String intercept(ActionInvocation paramActionInvocation) throws Exception; }
該接口定義了三個方法:
1,init():在該攔截器被初始化之后,在該攔截器執行攔截之前,系統將回調該方法,init()方法主要是用於打開一些資源,例如數據庫資源。該方法只執行一次。 2,destroy():該方法與init()方法對應,在攔截器銷毀之前,系統將回調該攔截器的destroy方法,該方法用於釋放init方法中打開的資源。 3,intercept(ActionInvocation paramActionInvocation):該方法是用戶需要攔截動作。就像Action的execute方法一樣,intercept方法會返回一個字符串作為邏輯視圖,如果該方法直接返回了一個字符串,系統將會跳轉到該邏輯視圖對應地實際視圖資源,不會調用被攔截的Action。該方法的(ActionInvocation 參數包含了被攔截的action的引用,可以通過調用該參數的invoke方法,將控制權轉給下一個攔截器,或者轉到action的exctute方法。 除此之外,Struts2還提供了一個com.opensymphony.xwork2.ActionInvocation.AbstractInterceptor抽象類,該類實現了com.opensymphony.xwork2.interceptor接口,則無需實現init和destory方法,自定義攔截器繼承com.opensymphony.xwork2.ActionInvocation.AbstractInterceptor,實現起來會更簡單
二:下面是一個簡單的控制登陸訪問的攔截器
/** * */ package com.test.demo.web.interceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.test.demo.constants.Constants; import com.test.demo.model.Userinfo; /** * Title: BaseDaoHibernateImpl Description: * * Copyright: Copyright (c) 2010 Company: demo * * @author zhengzhangwens * @version 1.0 * @since 2010-01-06 */ public class UserAuthorityInterceptor extends AbstractInterceptor { /* * (non-Javadoc) * * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com * .opensymphony.xwork2.ActionInvocation) */ @Override public String intercept(ActionInvocation invocation) throws Exception { // 取得請求相關的ActionContext實例 ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); // 取出名為user的session屬性 Userinfo user = (Userinfo) session.get(Constants.SESSION_USER); // 如果沒有登陸, 返回重新登陸 if (user != null) { return invocation.invoke(); } // 沒有登陸,將服務器提示設置成一個HttpServletRequest屬性 ctx.put("tip", "您還沒有登錄,請登陸系統"); return Action.LOGIN; } }
三:在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.devModel" value="true" /> <!-- 系統管理--> <package name="system" extends="struts-default" namespace="/system"> <!-- add by zhengzhangwen 2011-02-14 增加管理后台全局異常攔截與后台登陸驗證攔截 --> <!-- 配置應用所需要的攔截器 --> <interceptors> <!-- 全局異常攔截 --> <interceptor name="exceptionInterceptor" class="com.test.demo.web.interceptor.ExceptionInterceptor"> </interceptor> <!-- 后台登陸驗證攔截 --> <interceptor name="userAuthorityInterceptor" class="com.test.demo.web.interceptor.UserAuthorityInterceptor"> </interceptor> <!-- 攔截器棧的配置 --> <interceptor-stack name="systemInterceptorStack"> <interceptor-ref name="userAuthorityInterceptor" /> <interceptor-ref name="exceptionInterceptor" /> </interceptor-stack> </interceptors> <!-- 默認攔截器配置 覆蓋struts2中的默認攔截器 --> <default-interceptor-ref name="systemInterceptorStack"></default-interceptor-ref> <!-- 全局結果 --> <global-results> <result name="error">/error.jsp</result> <result name="login" type="redirect">/login.jsp</result> </global-results> <!-- 全局異常 --> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"> </exception-mapping> </global-exception-mappings> <!-- end --> <!-- 用戶登陸 --> <action name="login" class="loginAction" method="login"> <result name="success">/success.jsp</result> <result name="input">/error.jsp</result> </action> <!-- 用戶登出 --> <action name="logout" class="loginAction" method="logout"> <result name="success">/login.jsp</result> </action> </package> <!-- 辦公管理 --> <package name="official" extends="struts-default" namespace="/official"> </package> </struts>