@ExceptionHandler for all controllers in spring-mvc

大憨熊 提交于 2019-12-05 21:45:40

Change the way you annotate your controller from @Controller to @ControllerAdvice that will make it a global exception handler

the docs say

The default behavior (i.e. if used without any selector), the @ControllerAdvice annotated class will assist all known Controllers.

Also, you would have to change your method to something like

@ExceptionHandler(value = Exception.class)
public ModelAndView redirectToErrorPage(Exception e) {
    ModelAndView mav = new ModelAndView("errorPage");
    mav.getModelMap().addAttribute("message", "error on server");
    return mav;
}

To understand why the model argument is not resolved in the method's annotated with @ExceptionHandler check out the going deeper part of the http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!