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

2014-11-24 11:30:20 · 作者: · 浏览: 5
在开发的时候发现Struts2.16 在action内抛出异常的时候,控制台是没有打印信息的,不过在Struts2.0的版本却可以,还不知道为什么要去掉(暂时不去研究),但这样很不方面,特别是写AJax 调用的时候。。于是对 源码分析了一下后,发现了问题的所在是ExceptionMappingInterceptor 的默认参数的logEnabled 是false的,而抛出错误的时候根据这个判断决定是否打印。。
Java代码
protected boolean logEnabled = false;
public String intercept(ActionInvocation invocation) throws Exception {
String result;
try {
result = invocation.invoke();
} catch (Exception e) {
if (isLogEnabled()) {
handleLogging(e);
} www.2cto.com
List 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;
}
[java]
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 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代码
true
warn