struts错误不捕获也可以在控制台和日志文件输出

struts异常不捕获也可以在控制台和日志文件输出
在开发的时候发现Struts2.16 在action内抛出异常的时候,控制台是没有打印信息的,不过在Struts2.0的版本却可以,还不知道为什么要去掉(暂时不去研究),但这样很不方面,特别是写AJax 调用的时候。。于是对源码分析了一下后,发现了问题的所在是ExceptionMappingInterceptor 的默认参数的logEnabled 是false的,而抛出错误的时候根据这个判断决定是否打印。。

 

Java代码
protected boolean logEnabled = false;   
  1.   
  2. public String intercept(ActionInvocation invocation) throws Exception {   
  3.        String result;   
  4.   
  5.        try {   
  6.            result = invocation.invoke();   
  7.        } catch (Exception e) {   
  8.            if (isLogEnabled()) {   
  9.                handleLogging(e);   
  10.            }   
  11.            List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();   
  12.            String mappedResult = this.findResultFromExceptions(exceptionMappings, e);   
  13.            if (mappedResult != null) {   
  14.                result = mappedResult;   
  15.                publishException(invocation, new ExceptionHolder(e));   
  16.            } else {   
  17.                throw e;   
  18.            }   
  19.        }   
  20.   
  21.        return result;   
  22.    }  

 

 

了解了问题所在后,就知道怎么解决这个问题了。我的临时解决方案是在struts2的配置文件上加上下面:

Xml代码
  1. <interceptors>  
  2.             <interceptor-stack name="default">  
  3.                 <interceptor-ref name="exception">  
  4.                     <param name="logEnabled">true</param>  
  5.                     <param name="logLevel">  
  6.                         warn</param>  
  7.                 </interceptor-ref>  
  8.                 <interceptor-ref name="defaultStack"></interceptor-ref>  
  9.             </interceptor-stack>  
  10.         </interceptors>  
  11.         <default-interceptor-ref name="default"></default-interceptor-ref>