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.    }  
 protected boolean logEnabled = false;

 public String intercept(ActionInvocation invocation) throws Exception {
        String result;

        try {
            result = invocation.invoke();
        } catch (Exception e) {
            if (isLogEnabled()) {
                handleLogging(e);
            }
            List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
            String mappedResult = this.findResultFromExceptions(exceptionMappings, e);
            if (mappedResult != null) {
                result = mappedResult;
                publishException(invocation, new ExceptionHolder(e));
            } else {
                throw e;
            }
        }

        return result;
    }

 

 

了解了问题所在后,就知道怎么解决这个问题了。我的临时解决方案是在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>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值