org.springframework.web.servlet

Class ModelAndView

  • java.lang.Object
    • org.springframework.web.servlet.ModelAndView
    • Constructor Summary

      Constructors 
      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.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      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 ModelMap instance (never null).
      HttpStatus getStatus()
      Return the configured HTTP status for the response, if any.
      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 hasView()
      Indicate whether or not this ModelAndView has a view, either as a view name or as a direct View instance.
      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 clear() i.e.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ModelAndView

        public ModelAndView()
        Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.
        See Also:
        setView(View), setViewName(String)
      • ModelAndView

        public ModelAndView(java.lang.String viewName)
        Convenient constructor when there is no model data to expose. Can also be used in conjunction with addObject.
        Parameters:
        viewName - name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
        See Also:
        addObject(java.lang.String, java.lang.Object)
      • ModelAndView

        public ModelAndView(View view)
        Convenient constructor when there is no model data to expose. Can also be used in conjunction with addObject.
        Parameters:
        view - the View object to render
        See Also:
        addObject(java.lang.String, java.lang.Object)
      • ModelAndView

        public ModelAndView(java.lang.String viewName,
                            @Nullable
                            java.util.Map<java.lang.String,?> model)
        Create a new ModelAndView given a view name and a model.
        Parameters:
        viewName - name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
        model - a 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,
                            @Nullable
                            java.util.Map<java.lang.String,?> model)
        Create a new ModelAndView given a View object and a model. Note: the supplied model data is copied into the internal storage of this class. You should not consider to modify the supplied Map after supplying it to this class
        Parameters:
        view - the View object to render
        model - a 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,
                            HttpStatus status)
        Create a new ModelAndView given a view name and HTTP status.
        Parameters:
        viewName - name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
        status - an HTTP status code to use for the response (to be set just prior to View rendering)
        Since:
        4.3.8
      • ModelAndView

        public ModelAndView(@Nullable
                            java.lang.String viewName,
                            @Nullable
                            java.util.Map<java.lang.String,?> model,
                            @Nullable
                            HttpStatus status)
        Create a new ModelAndView given a view name, model, and HTTP status.
        Parameters:
        viewName - name of the View to render, to be resolved by the DispatcherServlet's ViewResolver
        model - a 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.
        status - an HTTP status code to use for the response (to be set just prior to View rendering)
        Since:
        4.3
      • 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's ViewResolver
        modelName - name of the single entry in the model
        modelObject - the single model object
      • ModelAndView

        public ModelAndView(View view,
                            java.lang.String modelName,
                            java.lang.Object modelObject)
        Convenient constructor to take a single model object.
        Parameters:
        view - the View object to render
        modelName - name of the single entry in the model
        modelObject - the single model object
    • Method Detail

      • setViewName

        public void setViewName(@Nullable
                                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

        @Nullable
        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.
      • setView

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

        @Nullable
        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.
      • hasView

        public boolean hasView()
        Indicate whether or not this ModelAndView has a view, either as a view name or as a direct View instance.
      • 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.
      • getModelInternal

        @Nullable
        protected java.util.Map<java.lang.String,java.lang.Object> getModelInternal()
        Return the model map. May return null. Called by DispatcherServlet for evaluation of the model.
      • getModelMap

        public ModelMap getModelMap()
        Return the underlying ModelMap instance (never null).
      • getModel

        public java.util.Map<java.lang.String,java.lang.Object> getModel()
        Return the model map. Never returns null. To be called by application code for modifying the model.
      • setStatus

        public void setStatus(@Nullable
                              HttpStatus status)
        Set the HTTP status to use for the response.

        The response status is set just prior to View rendering.

        Since:
        4.3
      • getStatus

        @Nullable
        public HttpStatus getStatus()
        Return the configured HTTP status for the response, if any.
        Since:
        4.3
      • isEmpty

        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.
      • wasCleared

        public boolean wasCleared()
        Return whether this ModelAndView object is empty as a result of a call to clear() i.e. whether it does not hold any view and does not contain a model.

        Returns false if any additional state was added to the instance after the call to clear().

        See Also:
        clear()
      • toString

        public java.lang.String toString()
        Return diagnostic information about this model and view.
        Overrides:
        toString in class java.lang.Object