When to use ModelAndView vs Model in Spring?

sheidaei :

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?

Akshay :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between model and modelandview in spring

ModelAndView.addObject vs Model.addAttribute

Java Spring adding model to ModelAndView throws StackOverflowException

How use content type ModelAndView in Spring MVC

ModelAndView reference to view with name model is null in spring boot

When to use model.predict(x) vs model(x) in tensorflow

When to use Spring Integration vs. Camel?

When should I use annotations vs. model methods in Django?

How to use Redirect in ModelAndView

Spring Boot vs. Groovy Templates - can't iterate over a list inside the ModelAndView

Why ModelMap, Model, @ModelAttribute, ModelAndView required when a simple return of jsp page name in string can do the work

When to use @ServerEndpoint vs @Controller in Java spring boot framework?

On returning ModelAndView in Spring, the url is not changed

Spring ModelAndView not working with ajax call

Spring MVC return modelandView class

Javascript reads my date String as number, when I pass my data with ModelAndView(Spring)

When to use Spring Prototype vs Request vs Session scope? Any real life example

When to use $ vs #?

When to use @ vs $ in R?

Django Rest Framework -A concrete example of when you would use a serializer class vs a model serializer class

View from ModelAndView vs View from ViewResolver

When to use ExponentialBackOffPolicy vs FixedBackOffPolicy when setting retry policy for a kafka consumer in a Spring boot app?

Spring Validation: Null ModelAndView returned to DispatcherServlet

Spring MVC 3: open ModelAndView in a new tab

Passing java variables to jsp with Spring @Controller ModelAndView

When to use an eloquent model in laravel

When to use ::query in Laravel model?

When to use autowiring in Spring

When does spring use application-dev.properties vs application.properties