---
title: "Struts 拦截器"
author: "q474818917"
date: "2012-06-13T14:25:09Z"
tags: ["Struts"]
categories: ["企业架构"]
slug: "struts"
draft: false
lastmod: "2012-06-13T14:25:56Z"
id: 1559965
url: "https://chinajweb.iteye.com/blog/1559965"
---

```
public String intercept(ActionInvocation invocation) {  
      //do some job before invocation  
      //...  
      String result = invocation.invoke();  
      //do some job after invocation  
      //...  
      return result;  
    }  
```

 使用自定义的Interceptor时，一定要定义一个Interceptor栈，定义你自己的拦截器和系统默认拦截器的调用顺序，如果直接在 action中引用你自定义的拦截器，就会发现只调用了你自己的拦截器，没有调用Struts2中内置的拦截器，那样有很多功能就会运行不正常了。

 

```
<interceptors> 
<interceptor name="authorize" class="com.struts2.interceptor.AuthorizeInterceptor" /> 
<interceptor-stack name="appStack">
                  <!-- 你自定义的 -->
    <interceptor-ref name="authorize"/> 
                  <!-- 系统内置的拦截器栈 -->
       <interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors> 
<action name="forward" class="com.struts2.RequestForward">
       <interceptor-ref name="appStack"/>
       <result name="index">index.jsp</result>
        <result name="NOT_FOUND">not_found.jsp</result>
</action>     
```

 

另附上保存Cookie：http://www.iteye.com/topic/149260