null). */
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
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
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();
}
}
测试代码如下:
| package com.sxt.web;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.sxt.po.User;
@Controller @RequestMapping("/user.do") publicclass UserControllerextends MultiActionController {
@RequestMapping(params="method=reg") public ModelAndView reg(String uname){ ModelAndView mv = new ModelAndView(); mv.setViewName("index"); // mv.setView(new RedirectView("index"));
User u = new User(); u.setUname("高淇"); mv.addObject(u); //查看源代码,得知,直接放入对象。属性名为”首字母小写的类名”。一般建议手动增加属性名称。 mv.addObject("a","aaaa"); returnmv; }
} |
| <%@ page language="java"import="java.util.*"pageEncoding="gbk"%> <%@ taglib prefix="c"uri="http://java.sun.com/js |