Redirect in Spring
This commit is contained in:
parent
3bb91d5409
commit
00dd88a0b8
|
@ -6,4 +6,4 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping)
|
- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping)
|
||||||
- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
|
- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
|
||||||
|
- [Redirect in Spring](http://www.baeldung.com/spring-redirect)
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package org.baeldung.redirect;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
import org.springframework.web.servlet.view.RedirectView;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/")
|
||||||
|
public class RedirectController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET)
|
||||||
|
public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) {
|
||||||
|
model.addAttribute("attribute", "redirectWithXMLConfig");
|
||||||
|
return new ModelAndView("RedirectedUrl", model);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET)
|
||||||
|
public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) {
|
||||||
|
model.addAttribute("attribute", "redirectWithRedirectPrefix");
|
||||||
|
return new ModelAndView("redirect:/redirectedUrl", model);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
|
||||||
|
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
|
||||||
|
redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
|
||||||
|
return new RedirectView("redirectedUrl");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET)
|
||||||
|
public RedirectView redirectWithUsingRedirectView(final ModelMap model) {
|
||||||
|
model.addAttribute("attribute", "redirectWithRedirectView");
|
||||||
|
return new RedirectView("redirectedUrl");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET)
|
||||||
|
public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) {
|
||||||
|
model.addAttribute("attribute", "redirectWithForwardPrefix");
|
||||||
|
return new ModelAndView("forward:/redirectedUrl", model);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET)
|
||||||
|
public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) {
|
||||||
|
model.addAttribute("redirectionAttribute", flashAttribute);
|
||||||
|
return new ModelAndView("redirection", model);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package org.baeldung.redirect;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
|
||||||
|
public class RedirectControllerTest {
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
protected WebApplicationContext wac;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
this.mockMvc = webAppContextSetup(this.wac).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig")))
|
||||||
|
.andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix")))
|
||||||
|
.andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes")))
|
||||||
|
.andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception {
|
||||||
|
this.mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue