You can return many things from the controller, and Spring will automatically try to determine what to do next.
When you return a ModelAndView, all the information that needs to be passed from the controller is embedded in it. No wonder there.
If you return String, it is assumed that this is the name of the view used. It will wrap this in a ModelAndView object with a string, since the view and the existing model are also embedded.
Spring does a lot of “magic” for return types, but also for parameter types. This allows you to write code in a more intuitive style for you, i.e. The following two examples are the same:
@RequestMapping(value="/") public ModelAndView mainPage() { Model m = new Model(); m.put("key", "value"); return new ModelAndView(m,"main"); }
and
@RequestMapping(value="/") public String mainPage(Model m) { m.put("key", "value"); return "main"; }
This second option is slightly less verbose and easier to test separately. The presented model will be an empty model (if not redirected from another controller).
Niels bech nielsen
source share