Model defines a holder for model attributes and is primarily designed for adding attributes to the model. 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 .
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.
There is no better way. Both are perfectly valid. Which one you choose to use depends which one suits your application better - Spring allows you to do it either way.
Historically, the two approaches come from different versions of Spring. The ModelAndView
approach was the primary way of returning both model and view information from a controller in pre-Spring 2.0. Now you can combine the Model
parameter and the String
return value, but the old approach is still valid.
I would like to add me 2 cents also. Second approach is more towards convention oriented i.e developer does explicitly mention what is his view but its implicit that return string is view name. So less coding,readable and standard. Much better than older way with ModelAndView
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