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?
Q : What is the difference between ModelMap and ModelAndView? Model is an interface while ModelMap is a class. ModelAndView is just a container for both a ModelMap and a View object. It allows a controller to return both as a single value.
ModelMap is an extension of Model with the ability to store attributes in a map and chain method calls. ModelAndView is a holder for a model and a view; it allows to return both model and view in one return value.
ModelAndView is a holder for both Model and View in the web MVC framework. These two classes are distinct; ModelAndView merely holds both to make it possible for a controller to return both model and view in a single return value. The view is resolved by a ViewResolver object; the model is data stored in a Map .
@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.
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());
}
@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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With