Allow MVC handler methods to return any CharSequence type as view name

Issue: SPR-13165
This commit is contained in:
Juergen Hoeller
2015-06-26 22:17:53 +02:00
parent a5349eb2f8
commit a2d3c27ed1
2 changed files with 46 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -39,16 +39,19 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
* Redirect scenarios including saving and retrieving flash attributes.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
*/
public class RedirectTests {
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = standaloneSetup(new PersonController()).build();
}
@Test
public void save() throws Exception {
this.mockMvc.perform(post("/persons").param("name", "Andy"))
@@ -60,6 +63,17 @@ public class RedirectTests {
.andExpect(flash().attribute("message", "success!"));
}
@Test
public void saveSpecial() throws Exception {
this.mockMvc.perform(post("/people").param("name", "Andy"))
.andExpect(status().isFound())
.andExpect(redirectedUrl("/persons/Joe"))
.andExpect(model().size(1))
.andExpect(model().attributeExists("name"))
.andExpect(flash().attributeCount(1))
.andExpect(flash().attribute("message", "success!"));
}
@Test
public void saveWithErrors() throws Exception {
this.mockMvc.perform(post("/persons"))
@@ -70,6 +84,16 @@ public class RedirectTests {
.andExpect(flash().attributeCount(0));
}
@Test
public void saveSpecialWithErrors() throws Exception {
this.mockMvc.perform(post("/people"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("persons/add"))
.andExpect(model().size(1))
.andExpect(model().attributeExists("person"))
.andExpect(flash().attributeCount(0));
}
@Test
public void getPerson() throws Exception {
this.mockMvc.perform(get("/persons/Joe").flashAttr("message", "success!"))
@@ -100,5 +124,16 @@ public class RedirectTests {
redirectAttrs.addFlashAttribute("message", "success!");
return "redirect:/persons/{name}";
}
@RequestMapping(value="/people", method=RequestMethod.POST)
public Object saveSpecial(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
if (errors.hasErrors()) {
return "persons/add";
}
redirectAttrs.addAttribute("name", "Joe");
redirectAttrs.addFlashAttribute("message", "success!");
return new StringBuilder("redirect:").append("/persons").append("/{name}");
}
}
}