见名知意,从名字上我们可以知道ModelAndView中的Model代表模型,View代表视图。即,这个类把要显示的数据存储到了Model属性中,要跳转的视图信息存储到了view属性。我们看一下ModelAndView的部分源码,即可知其中关系:

  1. public class ModelAndView {
  2.  
  3. /** View instance or view name String */
  4. private Object view;
  5.  
  6. /** Model Map */
  7. private ModelMap model;
  8.  
  9. /**
  10. * Indicates whether or not this instance has been cleared with a call to {@link #clear()}.
  11. */
  12. private boolean cleared = false;
  13.  
  14. /**
  15. * Default constructor for bean-style usage: populating bean
  16. * properties instead of passing in constructor arguments.
  17. * @see #setView(View)
  18. * @see #setViewName(String)
  19. */
  20. public ModelAndView() {
  21. }
  22.  
  23. /**
  24. * Convenient constructor when there is no model data to expose.
  25. * Can also be used in conjunction with <code>addObject</code>.
  26. * @param viewName name of the View to render, to be resolved
  27. * by the DispatcherServlet's ViewResolver
  28. * @see #addObject
  29. */
  30. public ModelAndView(String viewName) {
  31. this.view = viewName;
  32. }
  33.  
  34. /**
  35. * Convenient constructor when there is no model data to expose.
  36. * Can also be used in conjunction with <code>addObject</code>.
  37. * @param view View object to render
  38. * @see #addObject
  39. */
  40. public ModelAndView(View view) {
  41. this.view = view;
  42. }
  43.  
  44. /**
  45. * Creates new ModelAndView given a view name and a model.
  46. * @param viewName name of the View to render, to be resolved
  47. * by the DispatcherServlet's ViewResolver
  48. * @param model Map of model names (Strings) to model objects
  49. * (Objects). Model entries may not be <code>null</code>, but the
  50. * model Map may be <code>null</code> if there is no model data.
  51. */
  52. public ModelAndView(String viewName, Map<String, ?> model) {
  53. this.view = viewName;
  54. if (model != null) {
  55. getModelMap().addAllAttributes(model);
  56. }
  57. }
  58.  
  59. /**
  60. * Creates new ModelAndView given a View object and a model.
  61. * <emphasis>Note: the supplied model data is copied into the internal
  62. * storage of this class. You should not consider to modify the supplied
  63. * Map after supplying it to this class</emphasis>
  64. * @param view View object to render
  65. * @param model Map of model names (Strings) to model objects
  66. * (Objects). Model entries may not be <code>null</code>, but the
  67. * model Map may be <code>null</code> if there is no model data.
  68. */
  69. public ModelAndView(View view, Map<String, ?> model) {
  70. this.view = view;
  71. if (model != null) {
  72. getModelMap().addAllAttributes(model);
  73. }
  74. }
  75.  
  76. /**
  77. * Convenient constructor to take a single model object.
  78. * @param viewName name of the View to render, to be resolved
  79. * by the DispatcherServlet's ViewResolver
  80. * @param modelName name of the single entry in the model
  81. * @param modelObject the single model object
  82. */
  83. public ModelAndView(String viewName, String modelName, Object modelObject) {
  84. this.view = viewName;
  85. addObject(modelName, modelObject);
  86. }
  87.  
  88. /**
  89. * Convenient constructor to take a single model object.
  90. * @param view View object to render
  91. * @param modelName name of the single entry in the model
  92. * @param modelObject the single model object
  93. */
  94. public ModelAndView(View view, String modelName, Object modelObject) {
  95. this.view = view;
  96. addObject(modelName, modelObject);
  97. }
  98.  
  99. /**
  100. * Set a view name for this ModelAndView, to be resolved by the
  101. * DispatcherServlet via a ViewResolver. Will override any
  102. * pre-existing view name or View.
  103. */
  104. public void setViewName(String viewName) {
  105. this.view = viewName;
  106. }
  107.  
  108. /**
  109. * Return the view name to be resolved by the DispatcherServlet
  110. * via a ViewResolver, or <code>null</code> if we are using a View object.
  111. */
  112. public String getViewName() {
  113. return (this.view instanceof String ? (String) this.view : null);
  114. }
  115.  
  116. /**
  117. * Set a View object for this ModelAndView. Will override any
  118. * pre-existing view name or View.
  119. */
  120. public void setView(View view) {
  121. this.view = view;
  122. }
  123.  
  124. /**
  125. * Return the View object, or <code>null</code> if we are using a view name
  126. * to be resolved by the DispatcherServlet via a ViewResolver.
  127. */
  128. public View getView() {
  129. return (this.view instanceof View ? (View) this.view : null);
  130. }
  131.  
  132. /**
  133. * Indicate whether or not this <code>ModelAndView</code> has a view, either
  134. * as a view name or as a direct {@link View} instance.
  135. */
  136. public boolean hasView() {
  137. return (this.view != null);
  138. }
  139.  
  140. /**
  141. * Return whether we use a view reference, i.e. <code>true</code>
  142. * if the view has been specified via a name to be resolved by the
  143. * DispatcherServlet via a ViewResolver.
  144. */
  145. public boolean isReference() {
  146. return (this.view instanceof String);
  147. }
  148.  
  149. /**
  150. * Return the model map. May return <code>null</code>.
  151. * Called by DispatcherServlet for evaluation of the model.
  152. */
  153. protected Map<String, Object> getModelInternal() {
  154. return this.model;
  155. }
  156.  
  157. /**
  158. * Return the underlying <code>ModelMap</code> instance (never <code>null</code>).
  159. */
  160. public ModelMap getModelMap() {
  161. if (this.model == null) {
  162. this.model = new ModelMap();
  163. }
  164. return this.model;
  165. }
  166.  
  167. /**
  168. * Return the model map. Never returns <code>null</code>.
  169. * To be called by application code for modifying the model.
  170. */
  171. public Map<String, Object> getModel() {
  172. return getModelMap();
  173. }
  174.  
  175. /**
  176. * Add an attribute to the model.
  177. * @param attributeName name of the object to add to the model
  178. * @param attributeValue object to add to the model (never <code>null</code>)
  179. * @see ModelMap#addAttribute(String, Object)
  180. * @see #getModelMap()
  181. */
  182. public ModelAndView addObject(String attributeName, Object attributeValue) {
  183. getModelMap().addAttribute(attributeName, attributeValue);
  184. return this;
  185. }
  186.  
  187. /**
  188. * Add an attribute to the model using parameter name generation.
  189. * @param attributeValue the object to add to the model (never <code>null</code>)
  190. * @see ModelMap#addAttribute(Object)
  191. * @see #getModelMap()
  192. */
  193. public ModelAndView addObject(Object attributeValue) {
  194. getModelMap().addAttribute(attributeValue);
  195. return this;
  196. }
  197.  
  198. /**
  199. * Add all attributes contained in the provided Map to the model.
  200. * @param modelMap a Map of attributeName -> attributeValue pairs
  201. * @see ModelMap#addAllAttributes(Map)
  202. * @see #getModelMap()
  203. */
  204. public ModelAndView addAllObjects(Map<String, ?> modelMap) {
  205. getModelMap().addAllAttributes(modelMap);
  206. return this;
  207. }
  208.  
  209. /**
  210. * Clear the state of this ModelAndView object.
  211. * The object will be empty afterwards.
  212. * <p>Can be used to suppress rendering of a given ModelAndView object
  213. * in the <code>postHandle</code> method of a HandlerInterceptor.
  214. * @see #isEmpty()
  215. * @see HandlerInterceptor#postHandle
  216. */
  217. public void clear() {
  218. this.view = null;
  219. this.model = null;
  220. this.cleared = true;
  221. }
  222.  
  223. /**
  224. * Return whether this ModelAndView object is empty,
  225. * i.e. whether it does not hold any view and does not contain a model.
  226. */
  227. public boolean isEmpty() {
  228. return (this.view == null && CollectionUtils.isEmpty(this.model));
  229. }
  230.  
  231. /**
  232. * Return whether this ModelAndView object is empty as a result of a call to {@link #clear}
  233. * i.e. whether it does not hold any view and does not contain a model.
  234. * <p>Returns <code>false</code> if any additional state was added to the instance
  235. * <strong>after</strong> the call to {@link #clear}.
  236. * @see #clear()
  237. */
  238. public boolean wasCleared() {
  239. return (this.cleared && isEmpty());
  240. }
  241.  
  242. /**
  243. * Return diagnostic information about this model and view.
  244. */
  245. @Override
  246. public String toString() {
  247. StringBuilder sb = new StringBuilder("ModelAndView: ");
  248. if (isReference()) {
  249. sb.append("reference to view with name '").append(this.view).append("'");
  250. }
  251. else {
  252. sb.append("materialized View is [").append(this.view).append(']');
  253. }
  254. sb.append("; model is ").append(this.model);
  255. return sb.toString();
  256. }
  257. }

测试代码:

  1. package com.sxt.web;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.servlet.ModelAndView;
  6. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
  7.  
  8. import com.sxt.po.User;
  9.  
  10. @Controller
  11. @RequestMapping(value = "user")
  12. public class UserController extends MultiActionController {
  13.  
  14. @RequestMapping(value = "/reg")
  15. public ModelAndView reg(@RequestParam("uname") String uname){
  16. ModelAndView mv = new ModelAndView();
  17. mv.setViewName("index");
  18. // mv.setView(new RedirectView("index"));
  19.  
  20. User u = new User();
  21. u.setUname("高淇");
  22. mv.addObject(u); //查看源代码,得知,直接放入对象。属性名为”首字母小写的类名”。 一般建议手动增加属性名称。
  23. mv.addObject("a", "aaaa");
  24. return mv;
  25. }
  26.  
  27. }

jsp代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8. <h1>${requestScope.a}</h1>
  9. <h1>${requestScope.user.uname}</h1>
  10. </body>
  11. </html>

spring mvc 3.0 ModelAndView模型视图类的更多相关文章

  1. Spring MVC 3.0 深入及对注解的详细讲解

    核心原理 1.       用户发送请求给服务器.url:user.do 2.       服务器收到请求.发现Dispatchservlet可以处理.于是调用DispatchServlet. 3.  ...

  2. Spring MVC 3.0 深入及对注解的详细讲解[转载]

    http://blog.csdn.net/jzhf2012/article/details/8463783 核心原理 1.       用户发送请求给服务器.url:user.do 2.       ...

  3. 视图框架:Spring MVC 4.0(2)

    在<springMVC4(7)模型视图方法源码综合分析>一文中,我们介绍了ModelAndView的用法,它会在控制层方法调用完毕后作为返回值返回,里面封装好了我们的业务逻辑数据和视图对象 ...

  4. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)

    这一章大象将详细分析web层代码,以及使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能.     之前在使用Struts2实现MVC的注解时,是借助struts2-conventi ...

  5. spring MVC之构造ModelAndView对象

    spring MVC之构造ModelAndView对象 ---------- 构造ModelAndView对象 当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndV ...

  6. 2017.3.31 spring mvc教程(一)核心类与接口

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  7. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)

    在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...

  8. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...

  9. spring mvc 3.0 实现文件上传功能

    http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...

随机推荐

  1. 爬虫技术之——bloom filter(含java代码)

    在爬虫系统中,在内存中维护着两个关于URL的队列,ToDo队列和Visited队列,ToDo队列存放的是爬虫从已经爬取的网页中解析出来的即将爬取的URL,但是网页是互联的,很可能解析出来的URL是已经 ...

  2. bzoj 1492 [NOI2007]货币兑换Cash(斜率dp+cdq分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1492   [题意] 有AB两种货币,每天可以可以付IPi元,买到A券和B券,且A:B= ...

  3. signal()函数

    转自:http://blog.csdn.net/sddzycnqjn/article/details/7285760 1. 信号概念 信号是进程在运行过程中,由自身产生或由进程外部发过来的消息(事件) ...

  4. .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)

    1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/

  5. 桶排序-Node.js

    , , , , ]; var a = [], i; ; i < b.length; i++) { var num = b[i]; a[num] = a[num]||; a[num] ++; nu ...

  6. Strider安装(Ubuntu)

    安装: git clone https://github.com/Strider-CD/strider.git && cd strider nam install 然而还是出现一大波错 ...

  7. dom 拖拽div

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案

    png24在ie下支持透明.终于找到下面的可行办法: 我们知道 IE6 是不支持透明的 PNG-24 的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个 IE6 的透明 PNG-24 的方 ...

  9. UDP广域网,局域网通信-原理分析,穿透技术

    一.UDP局域网通信. 这个比较简单,关于局域网中的2台或者更多的计算机之间的UDP通信,网络上一大把,直接复制粘贴就可以使用,原理也非常简单.所以,本文不做详细介绍. 二.UDP广域通信(包括路由器 ...

  10. Spark RDD概念学习系列之RDD是什么?(四)

       RDD是什么? 通俗地理解,RDD可以被抽象地理解为一个大的数组(Array),但是这个数组是分布在集群上的.详细见  Spark的数据存储 Spark的核心数据模型是RDD,但RDD是个抽象类 ...