Spring MVC controller

In this example, let's look at a Spring MVC Controller method, returning both the model and View details in a single object using ModelAndView. The welcome method returns a ModelAndView object with the View name and appropriate attributes in the Model.

Take a look at the following controller:

    @Controller 
public class BasicModelViewController {
@RequestMapping(value = "/welcome-model-view")
public ModelAndView welcome(ModelMap model) {
model.put("name", "XYZ");
return new ModelAndView("welcome-model-view", model);
}
}

The following definitions explain the code in detail:

  • @RequestMapping(value = "/welcome-model-view"): The URI mapped is /welcome-model-view.
  • public ModelAndView welcome(ModelMap model): Note that the return value is no longer a string. It is a ModelAndView object.
  • return new ModelAndView("welcome-model-view", model): This creates a ModelAndView object with the appropriate View name and model.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset