---
title: "Struts2使用自定义拦截器"
author: "taeky"
date: "2010-07-30T15:20:01Z"
tags: ["Servlet", "Web", "框架"]
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2010-07-30T15:20:44Z"
id: 725468
url: "https://taeky.iteye.com/blog/725468"
---

虽然Struts2框架提供了许多拦截器，这些内建的拦截器实现了Struts2的大部分功能，因此，大部分Web应用的通用功能，都可以通过直接使用这些拦截器来完成。但还有一些系统逻辑相关的通用功能，则可以通过自定义拦截器来实现。值得称道的是，Struts2的拦截器系统是如此的简单，易用。

实现拦截器类
    如果用户要开发自己的拦截器类，应该实现com.opensymphony.xwork2.interceptor.Interceptor接口，该接口的类定义代码如下：

```java
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import java.io.Serializable;

public abstract interface Interceptor implements Serializable
{
    //销毁该拦截器之前的回调方法
    public abstract void destroy();

    //初始化该拦截器的回调方法
    public abstract void init();

    //拦截器实现拦截的逻辑方法
    public abstract String intercept(ActionInvocation paramActionInvocation)
        throws Exception;
}
```
通过上面的接口可以看出，该接口里包含了三个方法：
***init():** 在该拦截器被初始化之后，在该拦截器执行拦截之前，系统将回调该方法。对于每个拦截器而言，该init()方法只执行一次。因此，该方法的方法体主要用于打开一些一次性资源，例如数据库资源等。
***destroy():** 该方法与init()方法对应。在拦截器实例被销毁之前，系统将回调该拦截器的destroty方法，该方法用于销毁在init()方法里打开的资源。
***interceptor(ActionInvocation invocation):** 该方法是用户需要实现的拦截动作。就像Action的execute方法一样，intercept方法会返回一个字符串作为逻辑视图。如果该方法直接返回了一个字符串，系统将会跳转到该逻辑视图对应的实际视图资源，不会调用被拦截的Action。该方法的ActionInvocation参数包含了被拦截的Action的引用，可以通过调用该参数的invoke方法，将控制权转给下一个拦截器，或者转给Action的execute方法。

    除此之外，Struts2还提供了一个AbstractInterceptor类，该类提供了一个init和destory方法的空实现，如果我们实现的拦截器不需要申请资源，则可以无需实现这两个方法。可见，继承AbstractInterceptor类来实现自定义拦截器会更加简单。
AbstractInterceptor.java

```java
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;

public abstract class AbstractInterceptor
  implements Interceptor
{
  public void init()
  {
  }

  public void destroy()
  {
  }

  public abstract String intercept(ActionInvocation paramActionInvocation)
    throws Exception;
}
```

    下面实现了一个简单的拦截器：

```java
public class SimpleInterceptor extends AbstractInterceptor {

    // 简单拦截器的名字
    private String name;

    // 为该简单拦截器设置名字的setter方法
    public void setName(String name) {
        this.name = name;
    }

    // 拦截Action方法的intercept方法
    public String intercept(ActionInvocation invocation) throws Exception {
        // 取得被拦截的Action实例
        LoginAction action = (LoginAction) invocation.getAction();
        // 打印
        System.out.println(name + "拦截器的动作-----------  开始执行登录Action的时间为：" + new Date());
        // 取得开始执行Action的时间
        long start = System.currentTimeMillis();
        // 执行该拦截器的后一个拦截器，或者直接指定Action的execute方法
        String result = invocation.invoke();
        System.out.println(name + " 拦截器的动作-----------  执行完登录Action的时间为： " + new Date());
        // 取得执行完的事件
        long end = System.currentTimeMillis();
        System.out.println(name + "拦截器的动作-----------  执行完该Action的时间为：" + (end - start) + "毫秒");
        return result;
    }
}
```
    上面的拦截器仅仅在被拦截方法之前，打印出开始执行Action的时间，并记录开始执行Action的时刻；执行被拦截Action的execute方法之后，再次打印出当前时间，并输出执行Action的时长。
     当我们实现intercept(ActionInvocation invocation)方法之时，可以获得ActionInvocation参数，这个参数又可以获得被拦截的Action实例，一旦取得了Action实例，几乎获得了全部的控制权；可以实现将HTTP请求中的参数解析出来，设置成Action的属性（这是系统params拦截器干的事情）：也可以直接将HTTP请求中的HttpServletRequest实例和HttpServletResponse实例传给Action(这是servlet-config拦截器干的事情)……当然，也可实现应用相关的逻辑。