a、针对500异常的统一处理
1、实现HandlerExceptionResolver,重写resolveException方法package com.liying.mango.common.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;/** * 异常信息统一处理器 * * @author jqlin */public class MyExceptionHandler implements HandlerExceptionResolver { private static final Logger log = Logger.getLogger(MyExceptionHandler.class); @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) { //堆栈信息打印到错误的日志文件中 log.error(e.getMessage(), e); //跳转到500页面 return new ModelAndView("500"); }}
2、最后,将HandlerExceptionResolver实现类配置到Spring配置文件中
b、针对404异常的统一处理
web.xml 404配置404 /page/404.jsp