首页 > 代码库 > Spring MVC全局异常处理

Spring MVC全局异常处理

继承HandlerExceptionResolver接口实现自己的处理方法,如:

public class MyHandlerExceptionResolver implements HandlerExceptionResolver {   
  
    @Override  
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {   

        //添加自己的异常处理逻辑,如日志记录等        

         // TODO Auto-generated method stub   
        return new ModelAndView("exception");   
    }   
  
}

然后在项目的配置文件中添加:

<bean id="exceptionResolver" class="所在包名.MyHandlerExceptionResolver"/>

Spring MVC全局异常处理