Struts2 自定义拦截器

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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  <  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;
    }
    
}

你可能感兴趣的:(apache,log4j)