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.
This commit is contained in:
parent
be4e698483
commit
b0c735feae
|
|
@ -21,6 +21,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
import org.springframework.web.util.UrlPathHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -86,7 +87,8 @@ public abstract class AbstractUrlViewController extends AbstractController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the URL path to use for lookup and delegates to
|
* Retrieves the URL path to use for lookup and delegates to
|
||||||
* {@link #getViewNameForRequest}.
|
* {@link #getViewNameForRequest}. Also adds the content of
|
||||||
|
* {@link RequestContextUtils#getInputFlashMap} to the model.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
|
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
|
@ -95,7 +97,7 @@ public abstract class AbstractUrlViewController extends AbstractController {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
|
logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
|
||||||
}
|
}
|
||||||
return new ModelAndView(viewName);
|
return new ModelAndView(viewName, RequestContextUtils.getInputFlashMap(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Trivial controller that always returns a named view. The view
|
* <p>Trivial controller that always returns a named view. The view
|
||||||
|
|
@ -87,12 +88,13 @@ public class ParameterizableViewController extends AbstractController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a ModelAndView object with the specified view name.
|
* Return a ModelAndView object with the specified view name.
|
||||||
|
* The content of {@link RequestContextUtils#getInputFlashMap} is also added to the model.
|
||||||
* @see #getViewName()
|
* @see #getViewName()
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
|
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
return new ModelAndView(getViewName());
|
return new ModelAndView(getViewName(), RequestContextUtils.getInputFlashMap(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2012 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.servlet.mvc;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.servlet.FlashMapManager;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test fixture with a ParameterizableViewController.
|
||||||
|
*
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
* @since 3.1.1
|
||||||
|
*/
|
||||||
|
public class ParameterizableViewControllerTests {
|
||||||
|
|
||||||
|
private ParameterizableViewController controller;
|
||||||
|
|
||||||
|
private MockHttpServletRequest request;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
this.controller = new ParameterizableViewController();
|
||||||
|
this.request = new MockHttpServletRequest("GET", "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleRequestWithViewName() throws Exception {
|
||||||
|
String viewName = "testView";
|
||||||
|
this.controller.setViewName(viewName);
|
||||||
|
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
|
||||||
|
assertEquals(viewName, mav.getViewName());
|
||||||
|
assertTrue(mav.getModel().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleRequestWithoutViewName() throws Exception {
|
||||||
|
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
|
||||||
|
assertNull(mav.getViewName());
|
||||||
|
assertTrue(mav.getModel().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleRequestWithFlashAttributes() throws Exception {
|
||||||
|
this.request.setAttribute(FlashMapManager.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
|
||||||
|
ModelAndView mav = this.controller.handleRequest(this.request, new MockHttpServletResponse());
|
||||||
|
assertEquals(1, mav.getModel().size());
|
||||||
|
assertEquals("value", mav.getModel().get("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -20,8 +20,10 @@ import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.util.PathMatcher;
|
import org.springframework.util.PathMatcher;
|
||||||
|
import org.springframework.web.servlet.FlashMapManager;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
|
@ -150,6 +152,17 @@ public class UrlFilenameViewControllerTests extends TestCase {
|
||||||
assertTrue(mv.getModel().isEmpty());
|
assertTrue(mv.getModel().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testWithFlashAttributes() throws Exception {
|
||||||
|
UrlFilenameViewController ctrl = new UrlFilenameViewController();
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
|
||||||
|
request.setAttribute(FlashMapManager.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
ModelAndView mv = ctrl.handleRequest(request, response);
|
||||||
|
assertEquals("index", mv.getViewName());
|
||||||
|
assertEquals(1, mv.getModel().size());
|
||||||
|
assertEquals("value", mv.getModel().get("name"));
|
||||||
|
}
|
||||||
|
|
||||||
private void exposePathInMapping(MockHttpServletRequest request, String mapping) {
|
private void exposePathInMapping(MockHttpServletRequest request, String mapping) {
|
||||||
String pathInMapping = this.pathMatcher.extractPathWithinPattern(mapping, request.getRequestURI());
|
String pathInMapping = this.pathMatcher.extractPathWithinPattern(mapping, request.getRequestURI());
|
||||||
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathInMapping);
|
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathInMapping);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue