- 创建一个全局异常处理类
- 类上加上@ControllerAdvice注解
- 定义一个异常处理方法 方法上加@ExceptionHandler(value = Exception.class)
- 例子如下:
@ControllerAdvice
public class GlobalWebInterceptor {
@ExceptionHandler(value = Exception.class)
public ModelAndView intercept(HttpServletRequest req, Exception e) {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.setViewName("error");
return mav;
}
}
网友评论