---
title: "Struts2的拦截器过滤方法"
author: "fireflylover"
date: "2012-12-21T14:06:59Z"
tags: []
categories: ["Web前端"]
slug: "struts2"
draft: false
lastmod: "2012-12-21T14:06:40Z"
id: 1751687
url: "https://fireflylover.iteye.com/blog/1751687"
---

在Action中使用拦截器，默认情况下回拦截Action中所有的方法，但是在某些情况下，可能只需要拦截Action中的一个或多个方法，有时候也希望不拦截某个方法，这个在Struts2中是怎么实现的呢？

 

**拦截器方法过滤：让拦截器有选择的拦截Action中的某个方法！**

 

Struts2中提供了一个**MethodFilterInterceptor**类，开发者自定义的拦截器只需要继承该类就可以使用这个方法过滤的功能，来拦截Action中特定的方法！

 

**MethodFilterInterceptor**类是**AbstractInterceptor**拦截器的子类，实现了Interceptor和Serializable接口

 

MethodFilerInterceptor实现方法过滤中用到的两个参数

**execludeMethods：**该参数指定拦截器拒绝拦截的方法列表，多个方法用“，”隔开，指定了这个参数，拦截器不会拦截指定列表中的方法，就是所谓的黑名单 **includeMethods**：该参数指定拦截器需要拦截的方法列表，如果指定了参数，则指定的Action在执行前会被拦截，即白名单。

 

主要方法：

**①protected abstract String doIntercept(ActionInvocation invocation) throws Exception;  必须重写此方法，实现拦截。**

②String interceptor(ActionInvocation invocation):继承自AbstractInterceptor类，方法不需要强制重写

③void setExcludeMethods(String excludeMethods):设置拦截器黑名单，参数为Action一方法名。拦截器不拦截该方法

④void setIncludeMethods(String includeMethods):设置拦截器白名单，参数为Action一方法名。拦截器会拦截该方法

⑤Set&lt;String&gt; getExcludeMethodsSet()：获得拦截器的黑名单

⑥Set&lt;String&gt; getIncludeMethodsSet()：获得拦截器的白名单

 

**具体演示代码如下：**

**Action类：**

package com.accp.struts2.interceptor;

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction02 extends ActionSupport{

 private String name;  
 private String password;  
   
 public String getName() {  
  return name;  
 }

 public void setName(String name) {  
  this.name = name;  
 }

 public String getPassword() {  
  return password;  
 }

 public void setPassword(String password) {  
  this.password = password;  
 }

 @Override  
 public String execute() throws Exception {  
  System.out.println("this is execute method");  
  System.out.println("name = " + this.getName() + "tpassword = " + this.getPassword());  
  return SUCCESS;  
 }

 public String hello(){  
  System.out.println("this is hello method");  
  return "success";  
 }

 public String world(){  
  System.out.println("this is world method");  
  return "success";  
 }

}

 

**拦截器：**

package com.accp.struts2.interceptor;

import java.util.Map;

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

public class Interceptor02 extends MethodFilterInterceptor {

 @Override  
 protected String doIntercept(ActionInvocation arg0) throws Exception {  
  DemoAction02 da = (DemoAction02)arg0.getAction();  
  System.out.println(da.getClass().getName());

  Map&lt;String,Object&gt; session = arg0.getInvocationContext().getSession();  
  session.put("hello", "world");  
  System.out.println("interceptor02连接器之前------------");

  //执行该拦截器的后一个拦截器，或者直接指定Action的execute()方法   
  String retVal = arg0.invoke();  
  System.out.println("interceptor02连接器之后------------");  
  return retVal;  
 }  
}

**配置文件：**

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "[http://struts.apache.org/dtds/struts-2.0.dtd](http://struts.apache.org/dtds/struts-2.0.dtd)"&gt;

&lt;struts&gt;  
    &lt;package name="default\_intercptor02" namespace="/" extends="struts-default"&gt;  
     &lt;interceptors&gt;  
      &lt;interceptor name="demo02Interceptor" class="com.accp.struts2.interceptor.Interceptor02"&gt;&lt;/interceptor&gt;  
     &lt;/interceptors&gt;  
     &lt;action name="interceptor2" class="com.accp.struts2.interceptor.DemoAction02"&gt;  
            &lt;result name="success"&gt;chapter7/demo02\_result.jsp&lt;/result&gt;  
            &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt;  
            &lt;interceptor-ref name="demo02Interceptor"&gt;  
             &lt;param name="name"&gt;改名后的方法过滤拦截器&lt;/param&gt;  
             &lt;param name="excludeMethods"&gt;world&lt;/param&gt;   //不被拦截的方法名  
             &lt;param name="includeMethods"&gt;hello&lt;/param&gt;   //被拦截的方法名  
            &lt;/interceptor-ref&gt;  
        &lt;/action&gt;  
    &lt;/package&gt;  
&lt;/struts&gt;

**访问页面：**

 

&lt;%@ page language="java" import="java.util.\*" pageEncoding="GB18030"%&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;title&gt;My JSP 'demo01.jsp' starting page&lt;/title&gt;  
  &lt;/head&gt;

  &lt;body&gt;  
    &lt;form **action="../interceptor2!hello.action"** method="post"&gt;  
     name:&lt;input type="text" name="name"&gt;&lt;br/&gt;  
     password:&lt;input type="password" name="password"&gt;&lt;br/&gt;  
     &lt;input type="submit" value="提交"/&gt;  
    &lt;/form&gt;  
  &lt;/body&gt;  
&lt;/html&gt;

 

结果：

第一种：

&lt;form **action="../interceptor2!hello.action"** method="post"&gt;

 

com.accp.struts2.interceptor.DemoAction02  
interceptor02连接器之前------------  
this is hello method  
interceptor02连接器之后------------

 

第二种：

&lt;form **action="../interceptor2!world.action"** method="post"&gt;

com.accp.struts2.interceptor.DemoAction02  
this is world method