What is the difference between ModelAndView and ModelMap?

Is ModelMap only new name in Spring 3 for ModelAndView ?

Is functionality changing in Spring 3?

Consider this code in a Spring 3 application using ModelMap :

  @RequestMapping(value = "/order", method = RequestMethod.GET) public final String setup(final ModelMap model) { model.addAttribute(ORDER, new Order()); return "setup"; } 

I would like to know what the equivalent use of ModelAndView here ModelAndView be in an older Spring application? Would it just require a name change from ModelMap to ModelAndView to make this work in Spring 2.5?

+16
java spring spring-mvc
Jul 27 2018-10-10T00:
source share
2 answers

ModelAndView , as the name implies, contains the model and view name. ModelMap , in the contract, contains only information about the model.

Your example would be written (in the "old" Spring) as

  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { return new ModelAndView("setup", ORDER, new Order()); } 
+21
Jul 27 '10 at 2:33 p.m.
source share

@ coder247 Someone will correct me if I am wrong, but you do not need to return ModelMap. Add your attributes to your ModelMap and just return the string, which is the name of the view.

This is a great article explaining how to do this and more ... http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-annotation-example/

+8
Feb 07 '13 at 14:13
source share



All Articles