Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ModelAndView vs Model in Spring?

This might sound dumb to the Spring experts, but I have to ask:
How do you decide on when to use ModelAndView and when to use Model?

After all I have researched the best answer I have found is this one. It has mentioned that the ModelAndView is an old way and the Model with a String returned is a new way in Spring.

My question is shall we deprecate the old ModelAndView now that we have Model in hand? Or is there any cases that you need to use ModelAndView for it?

Also, does anyone know why the have to change ModelAndView to Model and String value as View, and what are the benefits?

like image 739
sheidaei Avatar asked Jun 05 '13 23:06

sheidaei


People also ask

When should I use ModelAndView and model?

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. I usually like ModelAndView to return the model and view from a controller.

Why we use ModelAndView in spring?

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 .

What is the use of model and view?

The model is responsible for managing the data of the application. It receives user input from the controller. The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects.

Which method on the ModelMap can be used to assign and send data back to the view?

The final interface to pass values to a view is the ModelAndView.


2 Answers

I always use the approach where controller methods return ModelAndView. Simply because it tends to make the controller methods a little more terse. The method parameters are now strictly input parameters. And all output related data is contained in the object returned from the method.

The ModelAndView style seems to resonate with people who don't like updating input parameters to a method. Sticking to the belief that this would constitute a side effect, a dangerous pattern because you cannot reliably predict what the method is going to do - It could return data in the returned object, or it could have updated anything in any of the input arguments.

So some people will still continue to prefer ModelAndView.

The new style with Model as method parameter and returned string as view name. Seems to have come from a slightly different design approach. Here the model objects are considered to be sort of events or items that are passed to multiple handlers, before being returned to the view where they are rendered. It reminds me how events are handled in the AWT / Swing world. This model is more coherent with the approach where multiple handlers could build on top of the Model objects, till it reaches a view.

So at the end of the day, there does not seem to be a definite reason to criticise or promote either approach. You should use the style that feels more consistent to your overall design philosophy.

Hope this helps.

like image 128
Akshay Avatar answered Oct 05 '22 23:10

Akshay


One difference I can spot is with ModelAndView object you can set a direct reference to a view object:

ModelAndView mav = ... mav.setView(myView); 

Whereas if you use Model and String, you need a view resolver to resolve the view name into an actual view

public String myHandler(...) {    return "myviewname"; // has to have a resolver from "myviewname" into an actual view } 
like image 35
gerrytan Avatar answered Oct 05 '22 21:10

gerrytan