原文地址: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. HDU 3848 CC On The Tree(树形dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3848 题意: 求一棵树上两个叶子结点之间的最短距离. 思路: 两个叶子节点之间一定会经过非叶子节点,除非只有两个 ...

  2. 字符串函数 mysql 和sqlserver 中对于字符串的常用函数和区别

    1. 对于字符串大小写的统一 mysql和sqlserver中都有同名函数lower()和upper(),但是mysql中还有另外一对函数,达到同样的目的,lcase()和ucase(),也就是英文中 ...

  3. Ajax_请求get,post案例

    1. 最原始的ajax请求方式 (1). get请求 <%@ Page Language="C#" AutoEventWireup="true" Code ...

  4. Qt5模型/视图结构-视图(View)

    实现自定义的View,可继承自QAbstractItemView类,对所需的纯虚函数进行重定义与实现,对于QAbstractItemView类中的纯虚函数,在子类中必须进行重定义,但不一定要实现,可根 ...

  5. [C#]读取不同版本的excel文件的方法

    --------------------------------2007及以上的版本-------------------------------- 测试如下: //DataInterface.Met ...

  6. sublime编辑器

    1.完全卸载sublime的方法. 打开preferences->browse packages 这个包,打开的那个目录,然后删除完整的sublime Text3目录就行. 2.sublime的 ...

  7. joomla 的语言翻译

    最近装了留言板组件 phocaguestbook,发觉没有中文翻译. 于是在 components\com_phocaguestbook\language\en-GB 找到了英文的文件. 依葫芦画瓢, ...

  8. 单细胞数据初步处理 | drop-seq | QC | 质控 | 正则化 normalization

    比对 The raw Drop-seq data was processed with the standard pipeline (Drop-seq tools version 1.12 from ...

  9. LeetCode--371--两整数之和

    问题描述: 方法: class Solution(object): def getSum(self, a, b): """ :type a: int :type b: i ...

  10. Web3.js 0.20.x API 中文版翻译

    文档原始链接为:https://web3.learnblockchain.cn/0.2x.x/,欢迎大家前往查阅,本文只是节选开头部分的介绍及API列表索引,以下为翻译正文: 为了开发一个基于以太坊的 ...