Spring MVC controller

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);       }    }

A few important things to note are as follows:

  • @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 ModelAndView.
  • return new ModelAndView("welcome-model-view", model): Create a ModelAndView object with the appropriate view name and model.

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.