1:
com.opensymphony.xwork2.interceptor.ExceptionMappingIntercep
tor
While you can configure exception mapping in your configuration file at any point, the configuration
* will not have any effect if this interceptor is not in the interceptor stack for your actions.
It is recommended that
* you make this interceptor the first interceptor on the stack,
ensuring that it has full access to catch any
* exception, even those caused by other interceptors.
这句话的意思是你可以配置异常在任何位置,只要这个拦截器没有加入生效的拦截器栈中,该拦截器就不会生效;
官方建议,将异常拦截器配置在拦截器的第一个位置,因为只有这样才能更好的捕获异常,甚至是
其他拦截器发生的异常;
@Override
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();
ExceptionMappingConfig mappingConfig =
this.findMappingFromException
s(exceptionMappings, e);
if (mappingConfig != null && mappingConfig.getResult()!=null) {
Map parameterMap = mappingConfig.getParams();
// create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable
invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap));
result = mappingConfig.getResult();
publishException(invocation, new ExceptionHolder(e));
} else {
throw e;
}
}