Struts2攔截器

攔截器實現了面向切面的組件,它將會影響多個業務對象的公共行爲封裝到一個個可重用的模塊,減小了系統的重複代碼,實現高度內聚,確保業務對象的整潔!java

攔截器與過濾器的區別:
攔截器和過濾器之間有不少相同之處,可是二者之間存在根本的差異。其主要區別爲如下幾點:
1)攔截器是基於Java反射機制的,而過濾器是基於函數回調的。
2)過濾器依賴於Servlet容器,而攔截器不依賴於Servlet容器
3)攔截器只能對Action請求起做用,而過濾器能夠對幾乎全部的請求起做用。
4)攔截器能夠訪問Action上下文、值棧裏的對象,而過濾器不能
5)在Action的生命週期中,攔截器能夠屢次被調用,而過濾器只能在容器初始化時被調用一次。spring

1、struts2內置攔截器

struts2在struts-default.xml中定義了不少經常使用的攔截器和攔截器棧apache

<interceptors>
	<!--攔截器 -->
    <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
    <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
    <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
    <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
    <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
    <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
    <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
    <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
    <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>
    <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
    <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
    <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
    <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
    <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
    <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
    <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
    <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
    <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
    <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
    <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
    <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
    <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
    <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
    <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
    <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
    <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
    <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
    <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
    <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
    <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
    <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
    <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
    <interceptor name="jsonValidation" class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor" />
    <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
    <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />

	<!--攔截器棧 -->
    <!-- Basic stack -->
    <interceptor-stack name="basicStack">
		<interceptor-ref name="exception"/>
		<interceptor-ref name="servletConfig"/>
		<interceptor-ref name="prepare"/>
		<interceptor-ref name="checkbox"/>
		<interceptor-ref name="multiselect"/>
		<interceptor-ref name="actionMappingParams"/>
		<interceptor-ref name="params">
		    <param name="excludeParams">dojo\..*,^struts\..*</param>
		</interceptor-ref>
		<interceptor-ref name="conversionError"/>
    </interceptor-stack>

    <!-- Sample validation and workflow stack -->
    <interceptor-stack name="validationWorkflowStack">
		<interceptor-ref name="basicStack"/>
		<interceptor-ref name="validation"/>
		<interceptor-ref name="workflow"/>
    </interceptor-stack>

    <!-- Sample JSON validation stack -->
    <interceptor-stack name="jsonValidationWorkflowStack">
		<interceptor-ref name="basicStack"/>
		<interceptor-ref name="validation">
		    <param name="excludeMethods">input,back,cancel</param>
		</interceptor-ref>
		<interceptor-ref name="jsonValidation"/>
		<interceptor-ref name="workflow"/>
    </interceptor-stack>

    <!-- Sample file upload stack -->
    <interceptor-stack name="fileUploadStack">
		<interceptor-ref name="fileUpload"/>
		<interceptor-ref name="basicStack"/>
    </interceptor-stack>

    <!-- Sample model-driven stack  -->
    <interceptor-stack name="modelDrivenStack">
		<interceptor-ref name="modelDriven"/>
		<interceptor-ref name="basicStack"/>
    </interceptor-stack>

    <!-- Sample action chaining stack -->
    <interceptor-stack name="chainStack">
		<interceptor-ref name="chain"/>
		<interceptor-ref name="basicStack"/>
    </interceptor-stack>

    <!-- Sample i18n stack -->
    <interceptor-stack name="i18nStack">
		<interceptor-ref name="i18n"/>
		<interceptor-ref name="basicStack"/>
    </interceptor-stack>

    <!-- An example of the paramsPrepareParams trick. This stack
	 is exactly the same as the defaultStack, except that it
	 includes one extra interceptor before the prepare interceptor:
	 the params interceptor.

	 This is useful for when you wish to apply parameters directly
	 to an object that you wish to load externally (such as a DAO
	 or database or service layer), but can't load that object
	 until at least the ID parameter has been loaded. By loading
	 the parameters twice, you can retrieve the object in the
	 prepare() method, allowing the second params interceptor to
	 apply the values on the object. -->
    <interceptor-stack name="paramsPrepareParamsStack">
		<interceptor-ref name="exception"/>
		<interceptor-ref name="alias"/>
		<interceptor-ref name="i18n"/>
		<interceptor-ref name="checkbox"/>
		<interceptor-ref name="multiselect"/>
		<interceptor-ref name="params">
		    <param name="excludeParams">dojo\..*,^struts\..*</param>
		</interceptor-ref>
		<interceptor-ref name="servletConfig"/>
		<interceptor-ref name="prepare"/>
		<interceptor-ref name="chain"/>
		<interceptor-ref name="modelDriven"/>
		<interceptor-ref name="fileUpload"/>
		<interceptor-ref name="staticParams"/>
		<interceptor-ref name="actionMappingParams"/>
		<interceptor-ref name="params">
		    <param name="excludeParams">dojo\..*,^struts\..*</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>

    <!-- A complete stack with all the common interceptors in place.
	 Generally, this stack should be the one you use, though it
	 may do more than you need. Also, the ordering can be
	 switched around (ex: if you wish to have your servlet-related
	 objects applied before prepare() is called, you'd need to move
	 servletConfig interceptor up.

	 This stack also excludes from the normal validation and workflow
	 the method names input, back, and cancel. These typically are
	 associated with requests that should not be validated.
	 -->
    <!--默認攔截器棧 -->
    <interceptor-stack name="defaultStack">
		<interceptor-ref name="exception"/>
		<interceptor-ref name="alias"/>
		<interceptor-ref name="servletConfig"/>
		<interceptor-ref name="i18n"/>
		<interceptor-ref name="prepare"/>
		<interceptor-ref name="chain"/>
		<interceptor-ref name="debugging"/>
		<interceptor-ref name="scopedModelDriven"/>
		<interceptor-ref name="modelDriven"/>
		<interceptor-ref name="fileUpload"/>
		<interceptor-ref name="checkbox"/>
		<interceptor-ref name="multiselect"/>
		<interceptor-ref name="staticParams"/>
		<interceptor-ref name="actionMappingParams"/>
		<interceptor-ref name="params">
		  <param name="excludeParams">dojo\..*,^struts\..*</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>

    <!-- The completeStack is here for backwards compatibility for
	 applications that still refer to the defaultStack by the
	 old name -->
    <interceptor-stack name="completeStack">
		<interceptor-ref name="defaultStack"/>
    </interceptor-stack>

    <!-- Sample execute and wait stack.
	 Note: execAndWait should always be the *last* interceptor. -->
    <interceptor-stack name="executeAndWaitStack">
		<interceptor-ref name="execAndWait">
		    <param name="excludeMethods">input,back,cancel</param>
		</interceptor-ref>
		<interceptor-ref name="defaultStack"/>
		<interceptor-ref name="execAndWait">
		    <param name="excludeMethods">input,back,cancel</param>
		</interceptor-ref>
    </interceptor-stack>

</interceptors>

其中默認攔截器是在不設置任何攔截器的時候,默認執行的。json

經過下面設置能夠改變默認攔截器cookie

<default-interceptor-ref name="攔截器棧名"/>

經過定義攔截器棧能夠選擇須要的攔截器。session

<!-- 自定義一個棧: 要引用默認棧、自定義的攔截器 -->
<interceptor-stack name="helloStack">
	<!-- 引用默認棧 (必定要放到第一行)-->
	<interceptor-ref name="defaultStack"></interceptor-ref>
	<!-- 引用自定義攔截器 -->
	<interceptor-ref name="helloInterceptor"></interceptor-ref>
</interceptor-stack>

2、自定義攔截器類

有兩種方式:app

  1. 實現 Interceptor 接口,實現 intercept 方法jsp

  2. 繼承 AbstractInterceptor類( 推薦 ),重寫 intercept 方法ide

示例函數

HelloInterceptor.java

package cn.iborder.a_interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import cn.iborder.log.MyLog;

/**
 * 自定義攔截器
 */
public class HelloInterceptor implements Interceptor{
	
	// 啓動時候執行
	public HelloInterceptor(){
		System.out.println(MyLog.getLog(this)+"建立了攔截器對象");
	}

	// 啓動時候執行
	@Override
	public void init() {
		System.out.println(MyLog.getLog(this)+"執行了攔截器的初始化方法");
	}

	// 攔截器業務處理方法 (在訪問action時候執行? 在execute以前執行?)
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println(MyLog.getLog(this)+"2. 執行Action以前");
		
		// 調用下一個攔截器或執行Action  (至關於chain.doFilter(..)
		// 獲取的是: execute方法的返回值
		String resultFlag = invocation.invoke();
		
		System.out.println(MyLog.getLog(this)+"4. 攔截器,業務處理-結束" + resultFlag);
		
		return resultFlag;
	}
	

	@Override
	public void destroy() {
		System.out.println(MyLog.getLog(this)+"銷燬....");
	}
}

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="hello" extends="struts-default">
		<!-- 【攔截器配置】 -->
		<interceptors>
			<!-- 配置用戶自定義的攔截器 -->
			<interceptor name="helloInterceptor" class="cn.iborder.a_interceptor.HelloInterceptor"></interceptor>
			
			<!-- 自定義一個棧: 要引用默認棧、自定義的攔截器 -->
			<interceptor-stack name="helloStack">
				<!-- 引用默認棧-->
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<!-- 引用自定義攔截器 -->
				<interceptor-ref name="helloInterceptor"></interceptor-ref>
			</interceptor-stack>
			
		</interceptors>
		
		<!-- 設置默認攔截器 -->
		<!--<default-interceptor-ref name="helloStack"></default-interceptor-ref>  -->
		
		<!-- Action配置 -->
		<action name="hello" class="cn.iborder.a_interceptor.HelloAction">
			<result name="success">/index.jsp</result>
			<!-- 調用helloStack -->
			<interceptor-ref name="helloStack"></interceptor-ref>
		</action>
	
	</package>
</struts>
相關文章
相關標籤/搜索