Spring-mvc controller and exception handling

后端 未结 5 1078
夕颜
夕颜 2021-02-09 08:33

Would like to ask you a best practice question where a spring-mvc controller is concerned. Please review the code below:

    @Autowired
    SomeService service;
         


        
5条回答
  •  后悔当初
    2021-02-09 09:06

    Define bean in bean definition file for Handler class. when any exception is thrown in a program ,resolveException method is called.

      public class Handler
            implements HandlerExceptionResolver
        {
    
            public Handler()
            {
            }
    
            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            {
                if(ex instanceof ErrorType1Exception))
                {
                     ModelAndView test = new ModelAndView("errorpage1jsppage");
    return test;
                } else
                if(ex instanceof ErrorType2Exception))
                {
                     ModelAndView test1 = new ModelAndView("errorpage2jsppage");
    return test1
                }
            }
        }
    

提交回复
热议问题