ModelAndView介绍
程序员文章站
2022-05-03 12:59:26
...
ModelAndView
案例见 public ModelAndView testModelAndView()
@RequestMapping(value="/testModelAndView",method=RequestMethod.GET)
public ModelAndView testModelAndView()
{
ModelAndView mv = new ModelAndView();
mv.addObject("myModelAndView",new java.text.SimpleDateFormat("yyyy-MM-dd
HH:mm:ss").format(new Date()));
mv.setViewName("ok");
return mv;
}
debug
结论
springmvc会将模型数据放进了request请求域里面,ModelAndView底层实际就是request.setAttribute(k,v)
protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest
request) throws Exception {
for (Map.Entry<String, Object> entry : model.entrySet()) {
String modelName = entry.getKey();
Object modelValue = entry.getValue();
if (modelValue != null) {
request.setAttribute(modelName, modelValue);
if (logger.isDebugEnabled()) {
logger.debug("Added model object '" + modelName + "' of type [" +
modelValue.getClass().getName() +
"] to request in view with name '" + getBeanName() + "'");
}
}
else {
request.removeAttribute(modelName);
if (logger.isDebugEnabled()) {
logger.debug("Removed model object '" + modelName +
"' from request in view with name '" + getBeanName() + "'");
}
}
}
}