1. Overview
In this article, we’ll look at the use of the core org.springframework.ui.Model, org.springframework.ui.ModelMap and org.springframework.web.servlet.ModelView provided by Spring MVC.
2. Maven Dependencies
Let’s start with the spring-context dependency in our pom.xml file:
1 2 3 4 5 |
|
The latest version of spring-context dependency can be found here.
For the ModelAndView, the spring-web dependency is required:
1 2 3 4 5 |
|
The latest versions of spring-web dependency can be found here.
And, if we use Thymeleaf as our view, we should add this dependency to pom.xml:
1 2 3 4 5 |
|
The latest version of Thymeleaf dependency can be found here.
3. Model
Let’s start with the most basic concept here – the Model.
Simply put, the model can supply attributes used for rendering views.
To provide a view with usable data, we simply add this data to its Model object. Additionally, maps with attributes can be merged with Model instances:
1 2 3 4 5 6 7 8 |
|
4. ModelMap
Just like the Model interface above, ModelMap is also used to pass values to render a view.
The advantage of ModelMap is it gives us the ability to pass a collection of values and treat these values as if they were within a Map:
1 2 3 4 5 6 |
|
5. ModelAndView
The final interface to pass values to a view is the ModelAndView.
This interface allows us to pass all the information required by Spring MVC in one return:
1 2 3 4 5 6 |
|
Constructor and Description |
---|
ModelAndView() Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments. |
ModelAndView(java.lang.String viewName) Convenient constructor when there is no model data to expose. |
ModelAndView(java.lang.String viewName, HttpStatus status) Create a new ModelAndView given a view name and HTTP status. |
ModelAndView(java.lang.String viewName, java.util.Map<java.lang.String,?> model) Create a new ModelAndView given a view name and a model. |
ModelAndView(java.lang.String viewName, java.util.Map<java.lang.String,?> model, HttpStatus status) Create a new ModelAndView given a view name, model, and HTTP status. |
ModelAndView(java.lang.String viewName, java.lang.String modelName, java.lang.Object modelObject) Convenient constructor to take a single model object. |
ModelAndView(View view) Convenient constructor when there is no model data to expose. |
ModelAndView(View view, java.util.Map<java.lang.String,?> model) Create a new ModelAndView given a View object and a model. |
ModelAndView(View view, java.lang.String modelName, java.lang.Object modelObject) Convenient constructor to take a single model object. |
ModelAndView |
addAllObjects(java.util.Map<java.lang.String,?> modelMap) Add all attributes contained in the provided Map to the model. |
ModelAndView |
addObject(java.lang.Object attributeValue) Add an attribute to the model using parameter name generation. |
ModelAndView |
addObject(java.lang.String attributeName, java.lang.Object attributeValue) Add an attribute to the model. |
void |
clear() Clear the state of this ModelAndView object. |
java.util.Map<java.lang.String,java.lang.Object> |
getModel() Return the model map. |
protected java.util.Map<java.lang.String,java.lang.Object> |
getModelInternal() Return the model map. |
ModelMap |
getModelMap() Return the underlying |
HttpStatus |
getStatus() Return the configured HTTP status for the response, if any. |
View |
getView() Return the View object, or |
java.lang.String |
getViewName() Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or |
boolean |
hasView() Indicate whether or not this |
boolean |
isEmpty() Return whether this ModelAndView object is empty, i.e. |
boolean |
isReference() Return whether we use a view reference, i.e. |
void |
setStatus(HttpStatus status) Set the HTTP status to use for the response. |
void |
setView(View view) Set a View object for this ModelAndView. |
void |
setViewName(java.lang.String viewName) Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver. |
java.lang.String |
toString() Return diagnostic information about this model and view. |
boolean |
wasCleared() Return whether this ModelAndView object is empty as a result of a call to |
6. The View
All the data, we place within these models, is used by a view – in general, a templated view to render the web page.
If we have a Thymeleaf template file targeted by our controller’s methods as their view. A parameter passed through the model will be accessible from within the thymeleaf HTML code:
1 2 3 4 5 6 7 8 9 |
|
The parameter passed here is used through the syntax ${message}, which is known as a placeholder. The Thymeleaf template engine will replace this placeholder with an actual value from an attribute of the same name passed through the model.