Skip to content

Commit b0c735f

Browse files
committed
SPR-8976 Make flash attrs available to view controllers.
Previously flash attributes were automatically merged into the model of annotated controllers only. This change extends the same benefit to ParameterizableView- and UrlFilenameViewController, both of which merely select a view without user controller logic and (the views) would otherwise not have access to the flash attributes.
1 parent be4e698 commit b0c735f

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/AbstractUrlViewController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.util.Assert;
2323
import org.springframework.web.servlet.ModelAndView;
24+
import org.springframework.web.servlet.support.RequestContextUtils;
2425
import org.springframework.web.util.UrlPathHelper;
2526

2627
/**
@@ -86,7 +87,8 @@ protected UrlPathHelper getUrlPathHelper() {
8687

8788
/**
8889
* Retrieves the URL path to use for lookup and delegates to
89-
* {@link #getViewNameForRequest}.
90+
* {@link #getViewNameForRequest}. Also adds the content of
91+
* {@link RequestContextUtils#getInputFlashMap} to the model.
9092
*/
9193
@Override
9294
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
@@ -95,7 +97,7 @@ protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpSer
9597
if (logger.isDebugEnabled()) {
9698
logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
9799
}
98-
return new ModelAndView(viewName);
100+
return new ModelAndView(viewName, RequestContextUtils.getInputFlashMap(request));
99101
}
100102

101103
/**

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/ParameterizableViewController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.servlet.http.HttpServletResponse;
2121

2222
import org.springframework.web.servlet.ModelAndView;
23+
import org.springframework.web.servlet.support.RequestContextUtils;
2324

2425
/**
2526
* <p>Trivial controller that always returns a named view. The view
@@ -87,12 +88,13 @@ public String getViewName() {
8788

8889
/**
8990
* Return a ModelAndView object with the specified view name.
91+
* The content of {@link RequestContextUtils#getInputFlashMap} is also added to the model.
9092
* @see #getViewName()
9193
*/
9294
@Override
9395
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
9496
throws Exception {
95-
return new ModelAndView(getViewName());
97+
return new ModelAndView(getViewName(), RequestContextUtils.getInputFlashMap(request));
9698
}
9799

98100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.mvc;
18+
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.springframework.mock.web.MockHttpServletRequest;
24+
import org.springframework.mock.web.MockHttpServletResponse;
25+
import org.springframework.ui.ModelMap;
26+
import org.springframework.web.servlet.FlashMapManager;
27+
import org.springframework.web.servlet.ModelAndView;
28+
29+
/**
30+
* Test fixture with a ParameterizableViewController.
31+
*
32+
* @author Rossen Stoyanchev
33+
* @since 3.1.1
34+
*/
35+
public class ParameterizableViewControllerTests {
36+
37+
private ParameterizableViewController controller;
38+
39+
private MockHttpServletRequest request;
40+
41+
@Before
42+
public void setup() {
43+
this.controller = new ParameterizableViewController();
44+
this.request = new MockHttpServletRequest("GET", "/");
45+
}
46+
47+
@Test
48+
public void handleRequestWithViewName() throws Exception {
49+
String viewName = "testView";
50+
this.controller.setViewName(viewName);
51+
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
52+
assertEquals(viewName, mav.getViewName());
53+
assertTrue(mav.getModel().isEmpty());
54+
}
55+
56+
@Test
57+
public void handleRequestWithoutViewName() throws Exception {
58+
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
59+
assertNull(mav.getViewName());
60+
assertTrue(mav.getModel().isEmpty());
61+
}
62+
63+
@Test
64+
public void handleRequestWithFlashAttributes() throws Exception {
65+
this.request.setAttribute(FlashMapManager.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
66+
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
67+
assertEquals(1, mav.getModel().size());
68+
assertEquals("value", mav.getModel().get("name"));
69+
}
70+
71+
}

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/UrlFilenameViewControllerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import org.springframework.mock.web.MockHttpServletRequest;
2222
import org.springframework.mock.web.MockHttpServletResponse;
23+
import org.springframework.ui.ModelMap;
2324
import org.springframework.util.AntPathMatcher;
2425
import org.springframework.util.PathMatcher;
26+
import org.springframework.web.servlet.FlashMapManager;
2527
import org.springframework.web.servlet.HandlerMapping;
2628
import org.springframework.web.servlet.ModelAndView;
2729

@@ -150,6 +152,17 @@ public void testNestedPathisUsedAsViewName_InBreakingChangeFromSpring12Line() th
150152
assertTrue(mv.getModel().isEmpty());
151153
}
152154

155+
public void testWithFlashAttributes() throws Exception {
156+
UrlFilenameViewController ctrl = new UrlFilenameViewController();
157+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
158+
request.setAttribute(FlashMapManager.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
159+
MockHttpServletResponse response = new MockHttpServletResponse();
160+
ModelAndView mv = ctrl.handleRequest(request, response);
161+
assertEquals("index", mv.getViewName());
162+
assertEquals(1, mv.getModel().size());
163+
assertEquals("value", mv.getModel().get("name"));
164+
}
165+
153166
private void exposePathInMapping(MockHttpServletRequest request, String mapping) {
154167
String pathInMapping = this.pathMatcher.extractPathWithinPattern(mapping, request.getRequestURI());
155168
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathInMapping);

0 commit comments

Comments
 (0)