17

Is ModelMap just the new name in Spring 3 for a ModelAndView?

Does the functionality change in Spring 3?

Consider this code in a Spring 3 app using a 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 here of ModelAndView would be in an older Spring app? Would it just require a name change from ModelMap to ModelAndView to get this working in Spring 2.5?

pnut butter
  • 367
  • 2
  • 5
  • 13

2 Answers2

24

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

Your example would have been written (in "old" Spring) as

 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
 {
  return new ModelAndView("setup", ORDER, new Order());
 }
skaffman
  • 398,947
  • 96
  • 818
  • 769
10

@coder247 Someone correct me if I'm wrong, but you don't need to return a ModelMap. Add your attributes to your ModelMap and simply return a String which is the View name.

This is an excellent article that explains how to do this and more... http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-annotation-example/

Rob Breidecker
  • 604
  • 1
  • 7
  • 12