欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

ModelAndView介绍

ModelAndView介绍

ModelAndView介绍

ModelAndView介绍

ModelAndView介绍

ModelAndView介绍

结论

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() + "'");
     }
   }
  }
 }