Skip to content

Commit 22015c8

Browse files
ahmetcetin39adamd1985
authored andcommitted
BAEL-1088 Model, ModelMap and ModelView in Spring MVC (eugenp#2590)
* Add files via upload * Controller methods are written. Test methods are written based on the controller methods and the view is created as an example.
1 parent 4cba067 commit 22015c8

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.controller.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.ui.ModelMap;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.servlet.ModelAndView;
9+
10+
/**
11+
* In this controller, Model, ModelMap and ModelAndView are shown as examples.
12+
* They are all used to pass parameters to JSP pages.
13+
* 04/09/2017
14+
*
15+
* @author Ahmet Cetin
16+
*/
17+
@Controller
18+
public class PassParametersController {
19+
@RequestMapping(value = "/showViewPage", method = RequestMethod.GET)
20+
public String passParametersWithModel(Model model) {
21+
model.addAttribute("message", "Baeldung");
22+
return "viewPage";
23+
}
24+
25+
@RequestMapping(value = "/printViewPage", method = RequestMethod.GET)
26+
public String passParametersWithModelMap(ModelMap map) {
27+
map.addAttribute("welcomeMessage", "welcome");
28+
map.addAttribute("message", "Baeldung");
29+
return "viewPage";
30+
}
31+
32+
@RequestMapping(value = "/goToViewPage", method = RequestMethod.GET)
33+
public ModelAndView passParametersWithModelAndView() {
34+
ModelAndView modelAndView = new ModelAndView("viewPage");
35+
modelAndView.addObject("message", "Baeldung");
36+
return modelAndView;
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>Title</title>
5+
</head>
6+
<body>
7+
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
8+
Web Application. Passed parameter : ${message}
9+
</div>
10+
</body>
11+
</html>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.baeldung.controller;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
import org.springframework.web.servlet.ModelAndView;
16+
17+
/**
18+
* This is the test class for {@link org.baeldung.controller.controller.PassParametersController} class.
19+
* 09/09/2017
20+
*
21+
* @author Ahmet Cetin
22+
*/
23+
@RunWith(SpringJUnit4ClassRunner.class)
24+
@WebAppConfiguration
25+
@ContextConfiguration({"classpath:test-mvc.xml"})
26+
public class PassParametersControllerTest {
27+
private MockMvc mockMvc;
28+
29+
@Autowired
30+
private WebApplicationContext wac;
31+
32+
@Before
33+
public void setUp() {
34+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
35+
}
36+
37+
@Test
38+
public void testPassParametersWithModel() throws Exception {
39+
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/showViewPage")).andReturn().getModelAndView();
40+
41+
//Validate view
42+
Assert.assertEquals(mv.getViewName(), "viewPage");
43+
44+
//Validate attribute
45+
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
46+
}
47+
48+
@Test
49+
public void testPassParametersWithModelMap() throws Exception {
50+
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/printViewPage")).andReturn().getModelAndView();
51+
52+
//Validate view
53+
Assert.assertEquals(mv.getViewName(), "viewPage");
54+
55+
//Validate attribute
56+
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
57+
}
58+
59+
@Test
60+
public void testPassParametersWithModelAndView() throws Exception {
61+
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/goToViewPage")).andReturn().getModelAndView();
62+
63+
//Validate view
64+
Assert.assertEquals(mv.getViewName(), "viewPage");
65+
66+
//Validate attribute
67+
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
68+
}
69+
}

0 commit comments

Comments
 (0)