在开发的时候发现Struts2.16 在action内抛出异常的时候,控制台是没有打印信息的,不过在Struts2.0的版本却可以,还不知道为什么要去掉(暂时不去研究),但这样很不方面,特别是写AJax 调用的时候。。于是对源码分析了一下后,发现了问题的所在是ExceptionMappingInterceptor 的默认参数的logEnabled 是false的,而抛出错误的时候根据这个判断决定是否打印。。
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的配置文件上加上下面:
<interceptors> <interceptor-stack name="default"> <interceptor-ref name="exception"> <param name="logEnabled">true</param> <param name="logLevel"> warn</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="default"></default-interceptor-ref>