org.springframework.web.servlet
Class ModelAndView

java.lang.Object
  extended byorg.springframework.web.servlet.ModelAndView

public class ModelAndView
extends java.lang.Object

Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.

Class to represent a model and view returned by a handler used by a DispatcherServlet. The view can take the form of a reference to a View object, or a String view name which will need to be resolved by a ViewResolver object. The model is a Map, allowing the use of multiple data objects keyed by name.

Author:
Rod Johnson, Juergen Hoeller
See Also:
DispatcherServlet, ViewResolver, HandlerAdapter.handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object), Controller.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Constructor Summary
ModelAndView(java.lang.String viewName)
          Convenient constructor when there is no model data to expose.
ModelAndView(java.lang.String viewName, java.util.Map model)
          Creates new ModelAndView given a view name and a model.
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 model)
          Creates 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.
 
Method Summary
 ModelAndView addObject(java.lang.String modelName, java.lang.Object modelObject)
          Add an object to the model.
 java.util.Map getModel()
          Return the model map.
 View getView()
          Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.
 java.lang.String getViewName()
          Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.
 boolean isReference()
          Return whether we use a view reference, i.e.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ModelAndView

public ModelAndView(View view)
Convenient constructor when there is no model data to expose.

Parameters:
view - View object to render

ModelAndView

public ModelAndView(java.lang.String viewName)
Convenient constructor when there is no model data to expose.

Parameters:
viewName - name of the View to render, to be resolved by the DispatcherServlet

ModelAndView

public ModelAndView(View view,
                    java.util.Map model)
Creates new ModelAndView given a View object and a model.

Parameters:
view - View object to render
model - Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.

ModelAndView

public ModelAndView(java.lang.String viewName,
                    java.util.Map model)
Creates new ModelAndView given a view name and a model.

Parameters:
viewName - name of the View to render, to be resolved by the DispatcherServlet
model - Map of model names (Strings) to model objects (Objects). Model entries may not be null, but the model Map may be null if there is no model data.

ModelAndView

public ModelAndView(View view,
                    java.lang.String modelName,
                    java.lang.Object modelObject)
Convenient constructor to take a single model object.

Parameters:
view - View object to render
modelName - name of the single entry in the model
modelObject - the single model object

ModelAndView

public ModelAndView(java.lang.String viewName,
                    java.lang.String modelName,
                    java.lang.Object modelObject)
Convenient constructor to take a single model object.

Parameters:
viewName - name of the View to render, to be resolved by the DispatcherServlet
modelName - name of the single entry in the model
modelObject - the single model object
Method Detail

addObject

public ModelAndView addObject(java.lang.String modelName,
                              java.lang.Object modelObject)
Add an object to the model.

Parameters:
modelName - name of the object to add to the model
modelObject - object to add to the model. May not be null.
Returns:
this ModelAndView, convenient to allow usages like return modelAndView.addObject("foo", bar);

setView

public void setView(View view)
Set a View object for this ModelAndView. Will override any pre-existing view name or View.


getView

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.


setViewName

public void setViewName(java.lang.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.


getViewName

public java.lang.String getViewName()
Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.


isReference

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.


getModel

public java.util.Map getModel()
Return the model map. May be null.


toString

public java.lang.String toString()
Return diagnostic information about this model and view.



Copyright (C) 2003-2004 The Spring Framework Project.