×
☰ See All Chapters

ModelAndView

ModelAndView is a class present in org.springframework.web.servlet package. ModelAndView is a value object designed to hold model and view making it possible for a handler to return both model and view in a single return value. ModelAndView is resolved by DispatcherServlet using special framework objects ViewResolver and View.

View

It is a String which is the name of a JSP file or which is the value given for the name attribute of a <bean> tag where this <bean> tag corresponds to the class which implements View interface.

Model

It is an object which stores the values that need to be sent to views/JSP. It can be a String object which has stored a string value. When we have to send many model values, we can store them in Map and object of Map can be used.

modelandview-0
 

Constructors of ModelAndView class

                Constructor

Description

ModelAndView()

Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.

ModelAndView(String viewName)

Convenient constructor when there is no model data to expose.

ModelAndView(String viewName, HttpStatus status)

Create a new ModelAndView given a view name and HTTP status.

ModelAndView(String viewName, Map< String,?> model)

Create a new ModelAndView given a view name and a model.

ModelAndView(String viewName, Map<String,?> model, HttpStatus status)

Create a new ModelAndView given a view name, model, and HTTP status.

ModelAndView(String viewName, String modelName, Object modelObject)

Convenient constructor to take a single model object.

ModelAndView(View view, String modelName, 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, Map< String,?> model)

Create a new ModelAndView given a View object and a model.

 

Important methods of ModelAndView class

Method

Description

public boolean hasView()

Indicate whether or not this ModelAndView has a view, either as a view name or as a direct View instance.

public boolean isEmpty()

Return whether this ModelAndView object is empty, i.e. whether it does not hold any view and does not contain a model.

public boolean isReference()

Return whether we use a view reference, i.e. true if the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver.

public Map getModel()

Return the model map. Never returns null. To be called by application code for modifying the model.

public ModelAndView addAllObjects( Map< String,?> modelMap)

Add all attributes contained in the provided Map to the model.

public ModelAndView addObject(String attributeName, Object attributeValue)

Add an attribute to the model.

public ModelMap getModelMap()

Return the underlying ModelMap instance (never null).

public String getViewName()

Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.

public View getView()

Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.

public void setView(View view)

Set a View object for this ModelAndView. Will override any pre-existing view name or View.

public void setViewName String viewName)

Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver. Will override any pre-existing view name or View.

 


All Chapters
Author