|
42 | 42 | import org.springframework.web.method.annotation.ModelMethodProcessor;
|
43 | 43 | import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
44 | 44 | import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
| 45 | +import org.springframework.web.servlet.DispatcherServlet; |
| 46 | +import org.springframework.web.servlet.FlashMap; |
45 | 47 | import org.springframework.web.servlet.ModelAndView;
|
| 48 | +import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
46 | 49 | import org.springframework.web.util.NestedServletException;
|
47 | 50 |
|
48 | 51 | import static org.junit.Assert.*;
|
|
53 | 56 | * @author Rossen Stoyanchev
|
54 | 57 | * @author Arjen Poutsma
|
55 | 58 | * @author Kazuki Shimizu
|
| 59 | + * @author Brian Clozel |
56 | 60 | * @since 3.1
|
57 | 61 | */
|
58 | 62 | @SuppressWarnings("unused")
|
@@ -82,6 +86,7 @@ public void setup() throws Exception {
|
82 | 86 | this.resolver = new ExceptionHandlerExceptionResolver();
|
83 | 87 | this.resolver.setWarnLogCategory(this.resolver.getClass().getName());
|
84 | 88 | this.request = new MockHttpServletRequest("GET", "/");
|
| 89 | + this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); |
85 | 90 | this.response = new MockHttpServletResponse();
|
86 | 91 | }
|
87 | 92 |
|
@@ -192,6 +197,20 @@ public void resolveExceptionModelAtArgument() throws Exception {
|
192 | 197 | assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName"));
|
193 | 198 | }
|
194 | 199 |
|
| 200 | + @Test // SPR-14651 |
| 201 | + public void resolveRedirectAttributesAtArgument() throws Exception { |
| 202 | + IllegalArgumentException ex = new IllegalArgumentException(); |
| 203 | + HandlerMethod handlerMethod = new HandlerMethod(new RedirectAttributesController(), "handle"); |
| 204 | + this.resolver.afterPropertiesSet(); |
| 205 | + ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); |
| 206 | + |
| 207 | + assertNotNull(mav); |
| 208 | + assertEquals("redirect:/", mav.getViewName()); |
| 209 | + FlashMap flashMap = (FlashMap) this.request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE); |
| 210 | + assertNotNull("output FlashMap should exist", flashMap); |
| 211 | + assertEquals("IllegalArgumentException", flashMap.get("exceptionClassName")); |
| 212 | + } |
| 213 | + |
195 | 214 | @Test
|
196 | 215 | public void resolveExceptionGlobalHandler() throws Exception {
|
197 | 216 | AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
|
@@ -364,6 +383,18 @@ public void handleException(Exception ex, Model model) {
|
364 | 383 | }
|
365 | 384 | }
|
366 | 385 |
|
| 386 | + @Controller |
| 387 | + static class RedirectAttributesController { |
| 388 | + |
| 389 | + public void handle() {} |
| 390 | + |
| 391 | + @ExceptionHandler |
| 392 | + public String handleException(Exception ex, RedirectAttributes redirectAttributes) { |
| 393 | + redirectAttributes.addFlashAttribute("exceptionClassName", ClassUtils.getShortName(ex.getClass())); |
| 394 | + return "redirect:/"; |
| 395 | + } |
| 396 | + } |
| 397 | + |
367 | 398 |
|
368 | 399 | @RestControllerAdvice
|
369 | 400 | @Order(1)
|
|
0 commit comments