拦截器,作用是在执行方法执行,进行过滤拦截。

struts2 拦截器的几种方式

  1. 声明拦截器,然后在Action中引用声明的拦截器

  2. 默认拦截器,在当前package下有且只能有一个默认拦截器

    作用:在当前package下所有的action都会经过默认拦截器拦截,当action中引入拦截器时,

    默认拦截器失效。

  3. 拦截器栈,把相关一组拦截器放在栈中,这样在action中就不必引入多个拦截器,直接引入拦截器栈就可以了,系统默认拦截器栈是 defaultStack ,如果自定义了拦截器栈,系统默认拦截器栈就不会起作用,这样我们需要手动引入系统默认拦截器栈,defaultStack

    注意:对于拦截器先后顺序,引入的前后就是拦截器的先后顺序。

  4. 自定义结果类型

    对于默认的结果类型是 dispatch  redirect  redirectAction chain



下面看下具体的实现步骤

  1. 介绍拦截器

package com.struts2.action;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

@SuppressWarnings("serial")
public class MyInter implements Interceptor{
	
	/*
	 * 在拦截器结束后,的销毁方法
	 * */
	@Override
	public void destroy() {
	}
	/**
	 * 已进入拦截器,初始化方法
	 */
	@Override
	public void init() {
	}
	/**
	 * 这里是拦截器的核心方法,写一些在执行action方法之前的一些逻辑
	 */
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		System.out.println("-----拦截器-------");
		ai.invoke();
		return null;
	}

}

2.拦截器写好了,我们看下配置,引入了自己的拦截器,系统默认就不会引用,需要添加上

<interceptors>
    <interceptor name="myInter" class="com.struts2.action.MyInter"/>
</interceptors>

 拦截器声明好了之后,如果想在某个action中用到拦截器,需要引用配置,配置信息如下

<action name="*_*" class="com.struts2.action.{1}" method="{2}">
    <!-- 引入拦截器 -->
  <interceptor-ref name="myInter"></interceptor-ref>
  <!-- 系统默认拦截器栈 -->
  <interceptor-ref name="defaultStack"></interceptor-ref>
    <result name="login">/login.jsp</result>
</action>

3.默认拦截器配置

<!-- 默认拦截器,当前package下的所有action都会执行这个拦截器,如果在action中引用了拦截器,
默认的就会失效
默认拦截器,可以是一个拦截器,也可以是一个拦截器栈
如果是一个拦截器,那么在action方法不会有系统默认拦截器栈,如果在action中引入系统默认拦截器,
那么默认拦截器又不会起作用,所有通常来说,默认拦截器写成拦截器栈,在默认拦截器栈中引入系统
默认拦截器
 -->
 <!-- 声明拦截器 -->
<interceptors>
    <interceptor name="myInter" class="com.struts2.action.MyInter"/>
    <interceptor name="myInter1" class="com.struts2.action.MyInter1"/>
    <interceptor name="myInter2" class="com.struts2.action.MyInter2"/>
    		
    	<!-- 拦截器栈,把多个拦截器大包,这样引用的时候,就可以直接引用拦截器栈名称 -->
    <interceptor-stack name="mi">
    <interceptor-ref name="myInter"/>
    <interceptor-ref name="myInter1"/>
    <interceptor-ref name="myInter2"/>
        <!-- 把系统默认拦截器栈引入进来  -->
	<interceptor-ref name="defaultStack"/></interceptor-stack>
    </interceptors>
    	
<!-- 默认拦截器,当前package下的所有action都会有这个拦截器,除非在action中引用拦截器,默认的就会失效 -->
<default-interceptor-ref name="mi"></default-interceptor-ref>

4.拦截器栈

<!-- 声明拦截器 -->
    	<interceptors>
    		<interceptor name="myInter" class="com.struts2.action.MyInter"/>
    		<interceptor name="myInter1" class="com.struts2.action.MyInter1"/>
    		<interceptor name="myInter2" class="com.struts2.action.MyInter2"/>
    		
    	<!-- 拦截器栈,把多个拦截器大包,这样引用的时候,就可以直接引用拦截器栈名称 -->
		<interceptor-stack name="mi">
		    <interceptor-ref name="myInter"/>
		    <interceptor-ref name="myInter1"/>
		    <interceptor-ref name="myInter2"/>
		</interceptor-stack>
    	</interceptors>
    	
    	
    	<action name="*_*" class="com.struts2.action.{1}" method="{2}">
    		
    		<!-- 引入拦截器栈 -->
    		<interceptor-ref name="mi"></interceptor-ref>
    		<!-- 引入系统默认拦截器栈,如果没有系统默认拦截器栈,
    		那么关于系统的方法就不能使用,比如,从页面传参,action接收不到 -->
    		<interceptor-ref name="defaultStack"></interceptor-ref>

    		<result name="login" type="redirect">/login.jsp</result>
    		<result name="index" type="redirect">/index.jsp</result>
    		<result name="error">/error.jsp</result>
    		
    	</action>
    	

5.自定义返回结果类型

 我们先实现自定义结果返回的跳转方式

package com.struts2.action;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.result.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;
/**
 * @author admin
 * 自定义拦截器结果类型
 */
@SuppressWarnings("serial")
public class CustomType extends StrutsResultSupport{
	/**
	 * 这里我们默认跳转方式为  重定向
	 */
	@Override
	protected void doExecute(String location, ActionInvocation invocation) 
			throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		String contexPath = ServletActionContext.getRequest().getContextPath();
		response.sendRedirect(contexPath+location);
	}
}

 看下如何配置

<!-- 自定义拦截器结果类型 -->
<result-types>
    <result-type name="defa" class="com.struts2.action.CustomType"></result-type>
</result-types>

<action name="*_*" class="com.struts2.action.{1}" method="{2}">
    <!--  这里type就是我们上面自定义的结果返回类型  -->
    <result name="login" type="defa">/login.jsp</result>
</action>


匆匆的写的,可能会有所疏漏,如有不足,请多多指导。