BAEL-1088 Model, ModelMap and ModelView in Spring MVC (#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.
This commit is contained in:
parent
4cba067620
commit
22015c8007
|
@ -0,0 +1,38 @@
|
|||
package org.baeldung.controller.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* In this controller, Model, ModelMap and ModelAndView are shown as examples.
|
||||
* They are all used to pass parameters to JSP pages.
|
||||
* 04/09/2017
|
||||
*
|
||||
* @author Ahmet Cetin
|
||||
*/
|
||||
@Controller
|
||||
public class PassParametersController {
|
||||
@RequestMapping(value = "/showViewPage", method = RequestMethod.GET)
|
||||
public String passParametersWithModel(Model model) {
|
||||
model.addAttribute("message", "Baeldung");
|
||||
return "viewPage";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/printViewPage", method = RequestMethod.GET)
|
||||
public String passParametersWithModelMap(ModelMap map) {
|
||||
map.addAttribute("welcomeMessage", "welcome");
|
||||
map.addAttribute("message", "Baeldung");
|
||||
return "viewPage";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/goToViewPage", method = RequestMethod.GET)
|
||||
public ModelAndView passParametersWithModelAndView() {
|
||||
ModelAndView modelAndView = new ModelAndView("viewPage");
|
||||
modelAndView.addObject("message", "Baeldung");
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
|
||||
Web Application. Passed parameter : ${message}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,69 @@
|
|||
package org.baeldung.controller;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* This is the test class for {@link org.baeldung.controller.controller.PassParametersController} class.
|
||||
* 09/09/2017
|
||||
*
|
||||
* @author Ahmet Cetin
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration({"classpath:test-mvc.xml"})
|
||||
public class PassParametersControllerTest {
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModel() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/showViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelMap() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/printViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelAndView() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/goToViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue