From 476864f3e91411b1f75c8777154c7d118341a6e3 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 1 Jul 2014 23:32:27 -0400 Subject: [PATCH] Expose handled exception as request attribute This change exposes exceptions handled in the DispatcherServlet with a HandlerExceptionResolver as a request attribute. This is done only when the resolver returns an empty ModelAndView indicating the exception was resolved but not view is required (e.g. status code was set). In such cases the exception may be useful to any handlers in an ERRPR dispatch by the servlet container. Issue: SPR-11686 --- .../springframework/web/servlet/DispatcherServlet.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index d7fc620a58..69bffd6b20 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -232,6 +232,13 @@ public class DispatcherServlet extends FrameworkServlet { */ public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER"; + /** + * Name of request attribute that exposes an Exception resolved with an + * {@link HandlerExceptionResolver} but where no view was rendered + * (e.g. setting the status code). + */ + public static final String EXCEPTION_ATTRIBUTE = DispatcherServlet.class.getName() + ".EXCEPTION"; + /** Log category to use when no mapped handler is found for a request. */ public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; @@ -1165,6 +1172,7 @@ public class DispatcherServlet extends FrameworkServlet { } if (exMv != null) { if (exMv.isEmpty()) { + request.setAttribute(EXCEPTION_ATTRIBUTE, ex); return null; } // We might still need view name translation for a plain error model...