接着上一篇
配置文件
<bean id="exceptionResolver" class="com.eroadsf.netdisk.core.MySimpleMappingExceptionResolver">
<property name="defaultErrorView" value="common_error" />
<property name="exceptionAttribute" value="exception" />
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">common_error</prop>
<prop key="java.lang.IllegalAccessException">no_access</prop>
</props>
</property>
</bean>
MySimpleMappingExceptionResolver
public class MySimpleMappingExceptionResolver extends SimpleMappingExceptionResolver {
private static final Logger logger = Logger.getLogger(MySimpleMappingExceptionResolver.class);
@Override
protected ModelAndView getModelAndView(String viewName, Exception ex, HttpServletRequest request) {
logger.error("exception",ex);
request.setAttribute("exception", ex);
return super.getModelAndView(viewName, ex, request);
}
}
2个错误页面
common_error.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html lang="en">
<head>
</head>
<body>
<div id="wrapper">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">错误异常</h3>
</div>
</div>
<!-- /.row -->
<div class="row">
<section>
<div class="col-lg-8 col-lg-offset-2">${exception}</div>
</section>
</div>
</div>
</div>
</body>
</html>
no_access.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="wrapper">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">无法访问</h3>
</div>
</div>
<div class="row">
<section>
<div class="col-lg-8 col-lg-offset-2">${exception}</div>
</section>
</div>
</div>
</div>
</body>
</html>
@RequestMapping(value = "/roleList")
public String role() {
int i=1/0;
return "manager/role";
}
@RequestMapping(value = "/test")
@ResponseBody
public void test() throws IllegalAccessException {
throw new java.lang.IllegalAccessException();
}
当访问到/roleList
错误页面跳转到common_error.jsp
当访问/test
错误页面跳转到no_access.jsp
在实际项目中就可以根据Controller层的异常跳转到不同的错误页面。
看看是怎么实现的。我这里自定义的异常处理是继承SimpleMappingExceptionResolver
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception {
//省略
//如果自己配置了自定义的HandlerExceptionResolver将会在这个方法里处理
mv = processHandlerException(request, response, handler, exception);
//省略
}
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
//遍历异常处理类
for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
if (exMv != null) {
break;
}
}
//省略
}
我这里自定义的异常处理是继承SimpleMappingExceptionResolver,
间接继承AbstractHandlerExceptionResolver
AbstractHandlerExceptionResolver
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
//省略
ModelAndView mav = doResolveException(request, response, handler, ex);
//省略
}
SimpleMappingExceptionResolver
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
// 根据异常选择异常要跳转的页面
String viewName = determineViewName(ex, request);
if (viewName != null) {
// Apply HTTP status code for error views, if specified.
// Only apply it if we're processing a top-level request.
Integer statusCode = determineStatusCode(request, viewName);
if (statusCode != null) {
applyStatusCodeIfPossible(request, response, statusCode);
}
//子类覆盖,可以增加对异常的处理比如记录异常日志
return getModelAndView(viewName, ex, request);
}
else {
return null;
}
}
根据配置参数exceptionMappings的列表和异常进行匹配,将匹配到的错误页面返回,没有匹配上返回默认错误页面
protected String determineViewName(Exception ex, HttpServletRequest request) {
String viewName = null;
if (this.excludedExceptions != null) {
for (Class<?> excludedEx : this.excludedExceptions) {
if (excludedEx.equals(ex.getClass())) {
return null;
}
}
}
//检查特定的异常映射
if (this.exceptionMappings != null) {
viewName = findMatchingViewName(this.exceptionMappings, ex);
}
// Return default error view else, if defined.
if (viewName == null && this.defaultErrorView != null) {
if (logger.isDebugEnabled()) {
logger.debug("Resolving to default view '" + this.defaultErrorView + "' for exception of type [" +
ex.getClass().getName() + "]");
}
viewName = this.defaultErrorView;
}
return viewName;
}
根据配置参数exceptionMappings的列表和异常进行匹配
protected String findMatchingViewName(Properties exceptionMappings, Exception ex) {
String viewName = null;
String dominantMapping = null;
int deepest = Integer.MAX_VALUE;
for (Enumeration<?> names = exceptionMappings.propertyNames(); names.hasMoreElements();) {
String exceptionMapping = (String) names.nextElement();
int depth = getDepth(exceptionMapping, ex);
if (depth >= 0 && (depth < deepest || (depth == deepest &&
dominantMapping != null && exceptionMapping.length() > dominantMapping.length()))) {
deepest = depth;
dominantMapping = exceptionMapping;
viewName = exceptionMappings.getProperty(exceptionMapping);
}
}
if (viewName != null && logger.isDebugEnabled()) {
logger.debug("Resolving to view '" + viewName + "' for exception of type [" + ex.getClass().getName() +
"], based on exception mapping [" + dominantMapping + "]");
}
return viewName;
}