原文地址:springMVC Model ModelMap 和 ModelAndView的区别

近来在看代码,发现controller里有不同的处理返回数据的方式,而自己一直在用ModelAndView在处理数据,对于其他的方式也零星用过,但是总感觉不明白其中的区别,也就写了这篇博客总结一下:

简单来说:

Model是包含四个addAttribute 和一个  merAttribute方法的接口。

ModelMap :实现了Map接口,包含Map方法。视图层通过request找到ModelMap中的数据。

ModelAndView:是包含ModelMap 和视图对象的容器。正如名字暗示的一样既包含模型也包含视图,而ModelMap只是包含模型的信息。

ModelAndView的例子,台后

public class CarListController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception { CarManager carManager = new CarManager(); ModelAndView modelAndView = new ModelAndView("carList");
modelAndView.addObject("carList", carManager.getCarList()); return modelAndView;
}
}

ModelAndView的例子,前台view

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Car List</h1> <c:forEach items="${carList}" var="car">
${car.brand.name} ${car.model}: ${car.price}
<br />
</c:forEach> </body>
<html>

ModelMap的例子:

public String testMethod(String someparam,ModelMap model)
{
//省略方法处理逻辑若干
//将数据放置到ModelMap对象model中,第二个参数可以是任何java类型
model.addAttribute("key",someparam);
...... //返回跳转地址
return "test/test";
}

或者直接使用接口:

public String toProvinceView(Model model, HttpSession session,) {  

            model.addAttribute("colModel", colModel);
model.addAttribute("colNames", colNames);
model.addAttribute("buttonName", buttonName);
return "statistic/StatisticChart";
}

参考资料:

http://error.news/question/231833/what-are-the-differences-between-model-modelmap-and-modelandview/

http://stackoverflow.com/questions/3344627/whats-the-difference-between-modelandview-and-modelmap

http://www.concretepage.com/spring/spring-mvc/spring-mvc-form-handling-example

springMVC Model ModelMap 和 ModelAndView的区别(转)的更多相关文章

  1. Model/ModelMap 和 ModelAndView 的区别使用

    Model/ModelMap 和 ModelAndView 的区别使用 Model/ModelMap controller: package springmvc.controller; import ...

  2. Spring中Model,ModelMap以及ModelAndView之间的区别

    原文链接:http://blog.csdn.net/zhangxing52077/article/details/75193948 Spring中Model,ModelMap以及ModelAndVie ...

  3. SpringMVC Map Model ModelMap 和 ModelAndView

    代码: package com.gaussic.controller; import com.gaussic.model.AccountModel; import org.springframewor ...

  4. Spring中Model、ModelMap及ModelAndView之间的区别

    Spring中Model.ModelMap及ModelAndView之间的区别   1. Model(org.springframework.ui.Model)Model是一个接口,包含addAttr ...

  5. Model、ModelMap、ModelAndView的使用和区别

    1.Model的使用 数据传递:Model是通过addAttribute方法向页面传递数据的: 数据获取:JSP页面可以通过el表达式或C标签库的方法获取数据: return:return返回的是指定 ...

  6. SpringMVC学习 -- ModelAndView , Model , ModelMap , Map 及 @SessionAttributes 的使用

    输出模型数据: ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息.方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 Model ...

  7. Model、ModelMap、ModelAndView的作用及区别

    Model.ModelMap.ModelAndView的作用及区别 对于MVC框架,控制器controller执行业务逻辑 用于产生模型数据Model 视图view用来渲染模型数据 Model和Mod ...

  8. springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系

    结论: a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同. b.当使用@ModelAttribute注解请求参数时,springmvc自动将该 ...

  9. ModelMap和ModelAndView区别

    首先介绍ModelMap和ModelAndView的作用 ModelMap ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可 ...

随机推荐

  1. Chrome控制台console的各种用法(方便调试)

    1.输出信息 console.log('消息内容!'); //输出普通信息 console.info('消息内容!'); //输出提示信息 console.error('消息内容!');//输出错误信 ...

  2. HDU 5442 Favorite Donut(暴力 or 后缀数组 or 最大表示法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 题意:给出一串字符串,它是循环的,现在要选定一个起点,使得该字符串字典序最大(顺时针和逆时针均可),如果有 ...

  3. Python 爬起数据时 'gbk' codec can't encode character '\xa0' 的问题

    1.被这个问题折腾了一上午终于解决了,再网上看到有用  string.replace(u'\xa0',u' ') 替换成空格的,方法试了没用. 后来发现 要在open的时候加utf-8才解决问题. 以 ...

  4. 小程序之从后台取到数据后放入想要的标签list里

    问题:事情是这样的,我有一个标签的功能,but   我怎么吧后台取到的数据放到我想要的标签里呢,而且是那种多个数据自己会加一个标签的内种,效果如下 解决:我们需要用到wx:for   这个东西呢是需要 ...

  5. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. (一)PHP简介

    什么是 PHP? PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词 PHP 是一种被广泛使用的开源脚本语言 PHP 脚本在服务器上执行 PHP ...

  7. STL_string.vector中find到的iterator的序号

    ZC:注意,printf("0x%08X\n",vtr.end()); 打印出来 应该就是 0x00000000,∵ 它就是 指向最后一个元素的后面,应该是理解成 无意义      ...

  8. Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 解决方法: 要对切割字符进行转义\\

    使用str.split("[",15)时,出现Exception in thread "main" java.util.regex.PatternSyntaxE ...

  9. 首篇 sdk 之 AlertDialog

    带着十足的干劲,用着有限的英语水平,我们来看看sdk里docs里的AlertDialog: AlertDialog SDK 原文描述:A dialog that can show a title, u ...

  10. Python全栈开发-Day10-进程/协程/异步IO/IO多路复用

    本节内容 多进程multiprocessing 进程间的通讯 协程 论事件驱动与异步IO Select\Poll\Epoll——IO多路复用   1.多进程multiprocessing Python ...