SPR-6464 Add RedirectModel

This commit is contained in:
Rossen Stoyanchev
2011-08-14 22:32:42 +00:00
parent 84ff2b49a6
commit 936c83b030
14 changed files with 541 additions and 288 deletions

View File

@@ -138,7 +138,7 @@ import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
import org.springframework.web.servlet.mvc.method.annotation.support.ServletWebArgumentResolverAdapter;
import org.springframework.web.servlet.mvc.method.support.ResponseContext;
import org.springframework.web.servlet.mvc.support.RedirectModel;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@@ -2816,13 +2816,14 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
@RequestMapping(value = "/messages", method = RequestMethod.POST)
public void sendMessage(TestBean testBean, BindingResult result, ResponseContext responseContext) {
public String sendMessage(TestBean testBean, BindingResult result, RedirectModel redirectModel) {
if (result.hasErrors()) {
responseContext.view("messages/new");
return "messages/new";
}
else {
responseContext.redirect("/messages/{id}").uriVariable("id", "1").queryParam("name", "value")
.flashAttribute("successMessage", "yay!");
redirectModel.addAttribute("id", "1").addAttribute("name", "value");
redirectModel.addFlashAttribute("successMessage", "yay!");
return "redirect:/messages/{id}";
}
}
}

View File

@@ -29,7 +29,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.support.RedirectModel;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;
/**
* Test fixture with {@link DefaultMethodReturnValueHandler}.
@@ -61,15 +63,38 @@ public class ViewMethodReturnValueHandlerTests {
public void returnView() throws Exception {
InternalResourceView view = new InternalResourceView("testView");
handler.handleReturnValue(view, createMethodParam("view"), mavContainer, webRequest);
assertSame(view, mavContainer.getView());
}
@Test
public void returnViewRedirect() throws Exception {
RedirectView redirectView = new RedirectView("testView");
RedirectModel redirectModel = new RedirectModel();
mavContainer.setRedirectModel(redirectModel);
handler.handleReturnValue(redirectView, createMethodParam("view"), mavContainer, webRequest);
assertSame(redirectView, mavContainer.getView());
assertSame("Should have switched to the RedirectModel", redirectModel, mavContainer.getModel());
}
@Test
public void returnViewName() throws Exception {
handler.handleReturnValue("testView", createMethodParam("viewName"), mavContainer, webRequest);
assertEquals("testView", mavContainer.getViewName());
}
@Test
public void returnViewNameRedirect() throws Exception {
RedirectModel redirectModel = new RedirectModel();
mavContainer.setRedirectModel(redirectModel);
handler.handleReturnValue("redirect:testView", createMethodParam("viewName"), mavContainer, webRequest);
assertEquals("redirect:testView", mavContainer.getViewName());
assertSame("Should have switched to the RedirectModel", redirectModel, mavContainer.getModel());
}
private MethodParameter createMethodParam(String methodName) throws Exception {
Method method = getClass().getDeclaredMethod(methodName);
return new MethodParameter(method, -1);

View File

@@ -0,0 +1,149 @@
/*
* Copyright 2002-2011 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.support;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.TestBean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.ui.ModelMap;
import org.springframework.validation.DataBinder;
/**
*
* Test fixture for RedirectModel tests.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class RedirectModelTests {
private RedirectModel redirectModel;
private FormattingConversionService conversionService;
private ModelMap implicitModel;
@Before
public void setup() {
this.conversionService = new DefaultFormattingConversionService();
DataBinder dataBinder = new DataBinder(null);
dataBinder.setConversionService(conversionService);
this.implicitModel = new ModelMap();
this.redirectModel = new RedirectModel(dataBinder, implicitModel);
}
@Test
public void addAttributePrimitiveType() {
this.redirectModel.addAttribute("speed", 65);
assertEquals("65", this.redirectModel.get("speed"));
}
@Test
public void addAttributeCustomType() {
String attrName = "person";
this.redirectModel.addAttribute(attrName, new TestBean("Fred"));
assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectModel.get(attrName));
this.conversionService.addConverter(new TestBeanConverter());
this.redirectModel.addAttribute(attrName, new TestBean("Fred"));
assertEquals("Type converter should have been used", "[Fred]", this.redirectModel.get(attrName));
}
@Test
public void addAttributeToString() {
String attrName = "person";
RedirectModel model = new RedirectModel();
model.addAttribute(attrName, new TestBean("Fred"));
assertEquals("toString() should have been used", "Fred", model.get(attrName));
}
@Test
public void addAttributeValue() {
this.redirectModel.addAttribute(new TestBean("Fred"));
assertEquals("Fred", this.redirectModel.get("testBean"));
}
@Test
public void addAllAttributesList() {
this.redirectModel.addAllAttributes(Arrays.asList(new TestBean("Fred"), new Integer(5)));
assertEquals("Fred", this.redirectModel.get("testBean"));
assertEquals("5", this.redirectModel.get("integer"));
}
@Test
public void addAttributesMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("person", new TestBean("Fred"));
map.put("age", 33);
this.redirectModel.addAllAttributes(map);
assertEquals("Fred", this.redirectModel.get("person"));
assertEquals("33", this.redirectModel.get("age"));
}
@Test
public void mergeAttributes() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("person", new TestBean("Fred"));
map.put("age", 33);
this.redirectModel.addAttribute("person", new TestBean("Ralph"));
this.redirectModel.mergeAttributes(map);
assertEquals("Ralph", this.redirectModel.get("person"));
assertEquals("33", this.redirectModel.get("age"));
}
@Test
public void addModelAttributes() {
this.implicitModel.addAttribute("person", new TestBean("Ralph"));
this.implicitModel.addAttribute("age", 33);
this.redirectModel.addModelAttributes("person", "age");
assertEquals("Ralph", this.redirectModel.get("person"));
assertEquals("33", this.redirectModel.get("age"));
}
@Test(expected=IllegalArgumentException.class)
public void addModelAttributesInvalidName() {
this.redirectModel.addModelAttributes("person");
}
public static class TestBeanConverter implements Converter<TestBean, String> {
public String convert(TestBean source) {
return "[" + source.getName() + "]";
}
}
}