SPR-6464 Add RedirectAttributes and SmartView interfaces.

This commit is contained in:
Rossen Stoyanchev
2011-08-25 07:50:07 +00:00
parent bd7fd57ad2
commit baea01ac90
11 changed files with 346 additions and 273 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.support.RedirectModel;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@@ -2816,13 +2816,13 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
@RequestMapping(value = "/messages", method = RequestMethod.POST)
public String sendMessage(TestBean testBean, BindingResult result, RedirectModel redirectModel) {
public String sendMessage(TestBean testBean, BindingResult result, RedirectAttributes redirectAttrs) {
if (result.hasErrors()) {
return "messages/new";
}
else {
redirectModel.addAttribute("id", "1").addAttribute("name", "value");
redirectModel.addFlashAttribute("successMessage", "yay!");
redirectAttrs.addAttribute("id", "1").addAttribute("name", "value")
.addFlashAttribute("successMessage", "yay!");
return "redirect:/messages/{id}";
}
}

View File

@@ -26,10 +26,12 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.ui.ModelMap;
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.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;
@@ -70,7 +72,7 @@ public class ViewMethodReturnValueHandlerTests {
@Test
public void returnViewRedirect() throws Exception {
RedirectView redirectView = new RedirectView("testView");
RedirectModel redirectModel = new RedirectModel();
ModelMap redirectModel = new RedirectAttributesModelMap();
mavContainer.setRedirectModel(redirectModel);
handler.handleReturnValue(redirectView, createMethodParam("view"), mavContainer, webRequest);
@@ -87,7 +89,7 @@ public class ViewMethodReturnValueHandlerTests {
@Test
public void returnViewNameRedirect() throws Exception {
RedirectModel redirectModel = new RedirectModel();
ModelMap redirectModel = new RedirectAttributesModelMap();
mavContainer.setRedirectModel(redirectModel);
handler.handleReturnValue("redirect:testView", createMethodParam("viewName"), mavContainer, webRequest);

View File

@@ -28,57 +28,53 @@ 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.
* Test fixture for {@link RedirectAttributesModelMap} tests.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class RedirectModelTests {
public class RedirectAttributesModelMapTests {
private RedirectModel redirectModel;
private RedirectAttributesModelMap redirectAttributes;
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);
this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
}
@Test
public void addAttributePrimitiveType() {
this.redirectModel.addAttribute("speed", 65);
assertEquals("65", this.redirectModel.get("speed"));
this.redirectAttributes.addAttribute("speed", 65);
assertEquals("65", this.redirectAttributes.get("speed"));
}
@Test
public void addAttributeCustomType() {
String attrName = "person";
this.redirectModel.addAttribute(attrName, new TestBean("Fred"));
this.redirectAttributes.addAttribute(attrName, new TestBean("Fred"));
assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectModel.get(attrName));
assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectAttributes.get(attrName));
this.conversionService.addConverter(new TestBeanConverter());
this.redirectModel.addAttribute(attrName, new TestBean("Fred"));
this.redirectAttributes.addAttribute(attrName, new TestBean("Fred"));
assertEquals("Type converter should have been used", "[Fred]", this.redirectModel.get(attrName));
assertEquals("Type converter should have been used", "[Fred]", this.redirectAttributes.get(attrName));
}
@Test
public void addAttributeToString() {
String attrName = "person";
RedirectModel model = new RedirectModel();
RedirectAttributesModelMap model = new RedirectAttributesModelMap();
model.addAttribute(attrName, new TestBean("Fred"));
assertEquals("toString() should have been used", "Fred", model.get(attrName));
@@ -86,17 +82,17 @@ public class RedirectModelTests {
@Test
public void addAttributeValue() {
this.redirectModel.addAttribute(new TestBean("Fred"));
this.redirectAttributes.addAttribute(new TestBean("Fred"));
assertEquals("Fred", this.redirectModel.get("testBean"));
assertEquals("Fred", this.redirectAttributes.get("testBean"));
}
@Test
public void addAllAttributesList() {
this.redirectModel.addAllAttributes(Arrays.asList(new TestBean("Fred"), new Integer(5)));
this.redirectAttributes.addAllAttributes(Arrays.asList(new TestBean("Fred"), new Integer(5)));
assertEquals("Fred", this.redirectModel.get("testBean"));
assertEquals("5", this.redirectModel.get("integer"));
assertEquals("Fred", this.redirectAttributes.get("testBean"));
assertEquals("5", this.redirectAttributes.get("integer"));
}
@Test
@@ -104,10 +100,10 @@ public class RedirectModelTests {
Map<String, Object> map = new HashMap<String, Object>();
map.put("person", new TestBean("Fred"));
map.put("age", 33);
this.redirectModel.addAllAttributes(map);
this.redirectAttributes.addAllAttributes(map);
assertEquals("Fred", this.redirectModel.get("person"));
assertEquals("33", this.redirectModel.get("age"));
assertEquals("Fred", this.redirectAttributes.get("person"));
assertEquals("33", this.redirectAttributes.get("age"));
}
@Test
@@ -116,27 +112,11 @@ public class RedirectModelTests {
map.put("person", new TestBean("Fred"));
map.put("age", 33);
this.redirectModel.addAttribute("person", new TestBean("Ralph"));
this.redirectModel.mergeAttributes(map);
this.redirectAttributes.addAttribute("person", new TestBean("Ralph"));
this.redirectAttributes.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");
assertEquals("Ralph", this.redirectAttributes.get("person"));
assertEquals("33", this.redirectAttributes.get("age"));
}
public static class TestBeanConverter implements Converter<TestBean, String> {