---
title: "Struts2 中的拦截器"
author: "kkcheng"
date: "2011-05-26T15:48:40Z"
tags: ["AOP", "Java", "Spring", "Webwork", "框架"]
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2011-05-26T15:48:40Z"
id: 1059788
url: "https://kkcheng.iteye.com/blog/1059788"
---

Struts2 中的拦截器和 servelt 中的过滤器是非常的相似的。如果学过过滤器的话，肯定能够感觉的到，尽管有些微的不同。可是struts2的拦截器到底如何使用呢，为什么会有这些配置呢？ 接下来一一来看。 过滤器和拦截器是非常相似的，过滤器 public interface Filter 接口里面有三个方法： init(FilterConfig filterConfig), destroy(), doFilter(ServletRequest request, ServletResponse response, FilterChain chain), 这里面的 doFilter() 方法是最重要的，在 struts2 中 String intercept(ActionInvocation invocation)就相当于此方法。 如何完成一个拦截器呢？在 struts2 中要实现一个接口 这个接口是什么呢？在哪呢？，是否在哪听说过？是 webwork 是我们以前听的最多的关于拦截器的框架， struts2 用了其中一个核心的东西，这个东西在是什么呢？是 xwork 。恩，有了它才可以拦截，好了我们在哪找呢？在 com.opensymphony.xwork2.interceptor 中找，里面有个 Interceptor 这是个接口，里面也有三个方法，有 init， destroy 和 intercept 三个方法，而在 struts2 里面的所有的拦截器都继承这个接口！ struts2接受客户端请求时，在执行action之前已经执行了自身默认的拦截器，这些拦截器完成struts2的大部分初始化的功能，比如设置属性值等。 执行完这些拦截器后，再执行action，返回客户端时，还得经过拦截器的拦截，最终返回客户端。下面我们开始模拟拦截器的实现过程： 1、新建自己的拦截器： view plaincopy to clipboardprint? package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:36:14 PM * 定义一个接口 \*/ public interface MyInterceptor { public void inteceptor(Invocation invocation) ; } package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:36:14 PM * 定义一个接口 \*/ public interface MyInterceptor { public void inteceptor(Invocation invocation) ; } 2、拦截器的具体实现 view plaincopy to clipboardprint? package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:41:26 PM * * spring * com.beckham.interceptors * FirstInterceptor.java \*/ public class FirstInterceptor implements MyInterceptor { public void inteceptor(Invocation invocation) { System.out.println("第一个拦截器开始。。。。"); //调用invoke方法 invocation.invoke() ; System.out.println("第一个拦截器结束......."); } } package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:41:26 PM * * spring * com.beckham.interceptors * FirstInterceptor.java \*/ public class FirstInterceptor implements MyInterceptor { public void inteceptor(Invocation invocation) { System.out.println("第一个拦截器开始。。。。"); //调用invoke方法 invocation.invoke() ; System.out.println("第一个拦截器结束......."); } } view plaincopy to clipboardprint? package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:42:22 PM * * spring * com.beckham.interceptors * SecondInterceptor.java \*/ public class SecondInterceptor implements MyInterceptor { public void inteceptor(Invocation invocation) { System.out.println("第二个拦截器开始....."); //调用invoke方法 invocation.invoke() ; System.out.println("第二个拦截器结束......"); } } package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:42:22 PM * * spring * com.beckham.interceptors * SecondInterceptor.java \*/ public class SecondInterceptor implements MyInterceptor { public void inteceptor(Invocation invocation) { System.out.println("第二个拦截器开始....."); //调用invoke方法 invocation.invoke() ; System.out.println("第二个拦截器结束......"); } } 3、需要执行的action view plaincopy to clipboardprint? package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:45:30 PM * * spring * com.beckham.interceptors * Action.java \*/ public class Action { public String execute() { System.out.println("执行execute方法"); return null ; } } package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:45:30 PM * * spring * com.beckham.interceptors * Action.java \*/ public class Action { public String execute() { System.out.println("执行execute方法"); return null ; } } 4、控制器，该控制器调配执行拦截器和action view plaincopy to clipboardprint? package com.beckham.interceptors; import java.util.ArrayList; import java.util.List; /\** * @author Owner * Jan 14, 2010 2:46:44 PM * * spring * com.beckham.interceptors * Invocation.java \*/ public class Invocation { List interceptors = new ArrayList(); int index = -1; public Invocation() { //需要执行的拦截器 this.interceptors.add(new FirstInterceptor()); this.interceptors.add(new SecondInterceptor()); } public void invoke() { index++; if (index &lt; interceptors.size()) { //一次调用拦截器的inteceptor方法 interceptors.get(index).inteceptor(this); } else { //拦截器拦截完毕后执行action new Action().execute(); } } } package com.beckham.interceptors; import java.util.ArrayList; import java.util.List; /\** * @author Owner * Jan 14, 2010 2:46:44 PM * * spring * com.beckham.interceptors * Invocation.java \*/ public class Invocation { List interceptors = new ArrayList(); int index = -1; public Invocation() { //需要执行的拦截器 this.interceptors.add(new FirstInterceptor()); this.interceptors.add(new SecondInterceptor()); } public void invoke() { index++; if (index &lt; interceptors.size()) { //一次调用拦截器的inteceptor方法 interceptors.get(index).inteceptor(this); } else { //拦截器拦截完毕后执行action new Action().execute(); } } } 5、main方法执行，在struts2框架中，前台的一个请求被过滤器拦截后就执行拦截，然后再执行action view plaincopy to clipboardprint? package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:47:32 PM * * spring * com.beckham.interceptors * Test.java \*/ public class Test { public static void main(String\[] args) { //struts2的拦截器入口就是invoke方法 new Invocation().invoke() ; } } package com.beckham.interceptors; /\** * @author Owner * Jan 14, 2010 2:47:32 PM * * spring * com.beckham.interceptors * Test.java \*/ public class Test { public static void main(String\[] args) { //struts2的拦截器入口就是invoke方法 new Invocation().invoke() ; } } 模拟结果： 第一个拦截器开始。。。。 第二个拦截器开始..... 执行execute方法 第二个拦截器结束...... 第一个拦截器结束....... 从模拟结果分析，拦截器相当于包裹在action外面的屏障，要达到执行action的目的，需要穿过屏障--------&gt;执行action--------&gt;穿出屏障 拦截器的实现其实是aop编程思想，在实现相应逻辑的前后，加上其他逻辑的实现，而且没有任何依赖关系。 本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/gaowenming/archive/2010/01/14/5189803.aspx