我如何使用@ExceptionHandler在spring中捕获异常

时间:2011-09-14 16:50:17

标签: spring exception-handling

我需要找到一种方法来捕获异常并将它们抛出到jsp页面

public class someController
    {
          @ExceptionHandler(Exception.class)
          @ResponseBody
          public void handleException(Exception exception,HttpServletRequest request, HttpServletResponse response) 
         {
            //how do I use this function to get exceptions and send them to
            //testModelView url below

         }

         @GET
         @Path("/testModelAndView")
         public ModelAndView resolveException()
         {

             ModelAndView mav = null;
             mav = new ModelAndView("/view.jsp");
             mav.addObject("/support/500.jsp", "Blah");

             return mav;
        }

1 个答案:

答案 0 :(得分:0)

您的异常处理程序方法可以返回ModelAndView。类似于:

@ExceptionHandler(TAAException.class)
public ModelAndView handleMyException(Exception exception) {
    logger.fatal(exception);
     ModelAndView mav = null;
         mav = new ModelAndView("/view.jsp");
         mav.addObject("/support/500.jsp", "Blah");
         return mav;
    return modelAndView;
}