Skip to content

Commit 6c6cc61

Browse files
author
eugenp
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
Conflicts: spring-rest/pom.xml spring-rest/src/main/webapp/WEB-INF/web.xml
2 parents a1e7be1 + cce68e0 commit 6c6cc61

7 files changed

Lines changed: 163 additions & 11 deletions

File tree

‎spring-rest/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
### Relevant Articles:
77
- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping)
88
- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
9-
9+
- [Redirect in Spring](http://www.baeldung.com/spring-redirect)

‎spring-rest/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
<scope>test</scope>
154154
</dependency>
155155

156+
<dependency>
157+
<groupId>org.springframework</groupId>
158+
<artifactId>spring-test</artifactId>
159+
<version>${spring.version}</version>
160+
</dependency>
161+
156162
</dependencies>
157163

158164
<build>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.baeldung.web.controller.redirect;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.ModelMap;
5+
import org.springframework.web.bind.annotation.ModelAttribute;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.servlet.ModelAndView;
9+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
10+
import org.springframework.web.servlet.view.RedirectView;
11+
12+
@Controller
13+
@RequestMapping("/")
14+
public class RedirectController {
15+
16+
@RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET)
17+
public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) {
18+
model.addAttribute("attribute", "redirectWithXMLConfig");
19+
return new ModelAndView("RedirectedUrl", model);
20+
}
21+
22+
@RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET)
23+
public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) {
24+
model.addAttribute("attribute", "redirectWithRedirectPrefix");
25+
return new ModelAndView("redirect:/redirectedUrl", model);
26+
}
27+
28+
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
29+
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
30+
redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
31+
redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
32+
return new RedirectView("redirectedUrl");
33+
}
34+
35+
@RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET)
36+
public RedirectView redirectWithUsingRedirectView(final ModelMap model) {
37+
model.addAttribute("attribute", "redirectWithRedirectView");
38+
return new RedirectView("redirectedUrl");
39+
}
40+
41+
@RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET)
42+
public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) {
43+
model.addAttribute("attribute", "redirectWithForwardPrefix");
44+
return new ModelAndView("forward:/redirectedUrl", model);
45+
}
46+
47+
@RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET)
48+
public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) {
49+
model.addAttribute("redirectionAttribute", flashAttribute);
50+
return new ModelAndView("redirection", model);
51+
}
52+
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
6+
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
7+
>
58

6-
<bean id="multipartResolver"
7-
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
9+
<context:component-scan base-package="org.baeldung.web" />
10+
11+
<mvc:annotation-driven />
12+
13+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
14+
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
15+
<property name="location">
16+
<value>/WEB-INF/spring-views.xml</value>
17+
</property>
18+
<property name="order" value="0" />
19+
</bean>
20+
21+
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
822
<!-- max upload size in bytes -->
923
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
10-
24+
1125
<!-- max size of file in memory (in bytes) -->
1226
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
13-
27+
1428
</bean>
15-
16-
</beans>
29+
30+
31+
</beans>
32+
33+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://www.springframework.org/schema/beans
3+
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
4+
>
5+
6+
<bean id="RedirectedUrl" class="org.springframework.web.servlet.view.RedirectView">
7+
<property name="url" value="redirectedUrl" />
8+
</bean>
9+
10+
</beans>

‎spring-rest/src/main/webapp/WEB-INF/web.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
<welcome-file/>
3939
</welcome-file-list>
4040

41-
</web-app>
41+
</web-app>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.baeldung.web.controller.redirect;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.nullValue;
5+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash;
7+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
12+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
13+
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
import org.springframework.test.context.web.WebAppConfiguration;
21+
import org.springframework.test.web.servlet.MockMvc;
22+
import org.springframework.web.context.WebApplicationContext;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml")
26+
@WebAppConfiguration
27+
public class RedirectControllerTest {
28+
29+
private MockMvc mockMvc;
30+
31+
@Autowired
32+
protected WebApplicationContext wac;
33+
34+
@Before
35+
public void setup() {
36+
mockMvc = webAppContextSetup(wac).build();
37+
}
38+
39+
@Test
40+
public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
41+
mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig")))
42+
.andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig"));
43+
}
44+
45+
@Test
46+
public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception {
47+
mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix")))
48+
.andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix"));
49+
}
50+
51+
@Test
52+
public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
53+
mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes")))
54+
.andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes"));
55+
}
56+
57+
@Test
58+
public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception {
59+
mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView"));
60+
}
61+
62+
@Test
63+
public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception {
64+
mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)