---
title: "Struts2 自定义拦截器"
author: "hwpok"
date: "2008-10-13T14:29:00Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2008-10-13T14:29:00Z"
id: 615818
url: "https://hwpok.iteye.com/blog/615818"
---

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;package org.roadway.wisp.zd.util;

import org.apache.log4j.Logger;

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

/\**  
 * @author Huyvanpull  
 *   
 \*/  
@SuppressWarnings("serial")  
public class ExceptionInterceptor extends AbstractInterceptor  
{  
    private Logger logger = Logger.getLogger(ExceptionInterceptor.class);  
      
    private String interceptorName;  
      
    @Override  
    public String intercept(ActionInvocation invocation) throws Exception  
    {  
        this.logger.debug("进入" + this.getInterceptorName());  
        String result = null;  
        try  
        {  
            result = invocation.invoke();  
        }  
        catch (Exception exception)  
        {  
            this.logger.error(this.getExceptionInfo(exception));  
            throw exception;  
        }  
        return result;  
    }  
      
    private String getExceptionInfo(Exception exception)  
    {  
        StringBuffer bExceptionInfo = new StringBuffer();  
        bExceptionInfo.append(exception.toString());  
        bExceptionInfo.append("\\n\\t");  
          
        StackTraceElement\[] stackTraceElements = exception.getStackTrace();  
        for (int i = 0; i &lt; stackTraceElements.length; i++)  
        {  
            bExceptionInfo.append("\[" + this.getInterceptorName() + "] "  
                    + stackTraceElements\[i].toString() + "\\n\\t");  
        }  
        return bExceptionInfo.toString();  
    }  
      
    public String getInterceptorName()  
    {  
        return interceptorName;  
    }  
      
    public void setInterceptorName(String interceptorName)  
    {  
        this.interceptorName = interceptorName;  
    }  
      
}