rnull
). */
public ModelMap getModelMap() {
if (this.model == null) {
this.model =new ModelMap();
}
returnthis.model;
}
/**
* Return the model map. Never returnsnull
.
* To be called by application code for modifying the model.
*/
public Map
getModel() {
return getModelMap();
}
/**
* Add an attribute to the model.
* @param attributeName name of the object to add to the model
* @param attributeva lue object to add to the model (nevernull
)
* @see ModelMap#addAttribute(String, Object)
* @see #getModelMap()
*/
publicModelAndView addObject(String attributeName, Object attributeva lue) {
getModelMap().addAttribute(attributeName, attributeva lue);
returnthis;
}
/**
* Add an attribute to the model using parameter name generation.
* @param attributeva lue the object to add to the model (nevernull
)
* @see ModelMap#addAttribute(Object)
* @see #getModelMap()
*/
publicModelAndView addObject(Object attributeva lue) {
getModelMap().addAttribute(attributeva lue);
returnthis;
}
/**
* Add all attributes contained in the provided Map to the model.
* @param modelMap a Map of attributeName-> attributeva lue pairs
* @see ModelMap#addAllAttributes(Map)
* @see #getModelMap()
*/
publicModelAndView addAllObjects(Map
modelMap) {
getModelMap().addAllAttributes(modelMap);
returnthis;
}
/**
* Clear the state of this ModelAndView object.
* The object will be empty afterwards.
*
Can be used to suppress rendering of a given ModelAndView object
* in the postHandle
method of a HandlerInterceptor.
* @see #isEmpty()
* @see HandlerInterceptor#postHandle
*/
publicvoid clear() {
this.view =null;
this.model =null;
this.cleared =true;
}
/**
* Return whether this ModelAndView object is empty,
* i.e. whether it does not hold any view and does not contain a model.
*/
publicboolean isEmpty() {
return (this.view == null && CollectionUtils.isEmpty(this.model));
}
/**
* Return whether this ModelAndView object is empty as a result of a call to{@link #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{@link #clear}.
* @see #clear()
*/
publicboolean wasCleared() {
return (this.cleared && isEmpty());
}
/**
* Return diagnostic information about this model and view.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ModelAndView: ");
if (isReference()) {
sb.append("reference to view with name '").append(this.view).append("'");
}
else {
sb.append("materialized View is [").append(this.view).append(']');
}
sb.append("; model is ").append(this.model);
return sb.toString();
}
}