结论:

a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同。

b.当使用@ModelAttribute注解请求参数时,springmvc自动将该参数放入model,modelmap,map中。

c.model,modelmap,map中put,request.setattribute(),b中@ModelAttribute以及modelandveiw.addObj()效果相同,return时都是将参数放request的attribute中。

d.model,modelmap,map,modelandview的生命同期仅存在于当前方法内,forward/return后重新生成新空对象。

e.当使用redirectAttribute.addFlashAttribute重定向时,FlashAttribute会自动注入下一个action内部model,modelmap,map,详细请参考FlashAtrribute详细中的3.2model跟踪。

1.发送请求:http://localhost:8080/project/page/login/ModelTest/Map.do?aa=111&bb=333

@Controller
@RequestMapping("/page/login/ModelTest")
public class ModelTestController {
@RequestMapping(value = "/Map.do")
public String MapTest(HttpServletRequest request, Map<String, Object> map) {
System.out.println("hello");
System.out.println(map);
map.put("step1", "step1");
PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName());//打印
map.put("step2", "step2");
request.setAttribute("step3", "step3");
final HttpSession session = request.getSession();
session.setAttribute("step4", "step4");
// return "../loginSuccess.jsp";
return "Map1.do"; }
}

输出:
hello
{}
================com..controller.ModelTestController====================
print  request parameter:
aa==111
bb==333
print  request attribute:

print  session parameter:
step4==step4

结论:map的初始值为空

2.当请求forward到第二个action(Map1.do)中

@RequestMapping(value = "/Map1.do")
public ModelAndView MapTest1(HttpServletRequest request, @ModelAttribute("aa") String aa,
Map<String, Object> map, Model model) {
     ModelAndView mav = new ModelAndView();
System.out.println("welcome to MapTest1");
model.addAttribute("mdbefore", "before");
System.out.println(map);
System.out.println("................");
System.out.println(model.asMap()); model.addAttribute("mdafter", "after"); System.out.println("hello");
System.out.println(map);
System.out.println("................");
System.out.println(model.asMap()); PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName() + "1"); mav.addObject("name", "mike");
      mav.setViewName("Map2.do");
      // return "Map2.do";
      return mav;
     //return new ModelAndView("Map2.do","name","mike");//红色部分代码可用这一句代替
}

输出:

welcome to MapTest1
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before}

................//aa由形参@ModelAttribute(“aa”)注入
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before}
hello
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before, mdafter=after}
................
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before, mdafter=after}
================com.controller.ModelTestController1====================
print  request parameter:
aa==111
bb==333
print  request attribute:

step3==step3 //上一个action中map.put加入
step2==step2
step1==step1 //上一个action中request.setattribute加入

print  session parameter:
step4==step4

结论:

a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同。

b.当使用@ModelAttribute注解请求参数时,springmvc自动将该参数放入model,modelmap,map中。

3.当请求进入第三个action(Map2.do)时

@RequestMapping(value = "/Map2.do")
public String MapTest2(HttpServletRequest request, Map<String, Object> map, Model model,
ModelMap mm) {
System.out.println("welcome to MapTest2");
model.addAttribute("mdbefore2", "before2");
System.out.println(map);
System.out.println("................");
System.out.println(model.asMap());
System.out.println("................");
System.out.println(mm);
model.addAttribute("mdafter2", "after2"); System.out.println("hello");
System.out.println(map);
System.out.println("................");
System.out.println(model.asMap()); PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName() + "2");
return "../loginSuccess.jsp";
}

输出结果:

welcome to MapTest2
{mdbefore2=before2}
................
{mdbefore2=before2}
................
{mdbefore2=before2}
hello
{mdbefore2=before2, mdafter2=after2}
................
{mdbefore2=before2, mdafter2=after2}
================com.controller.ModelTestController2====================
print  request parameter:
aa==111
bb==333
print  request attribute:

mdbefore==before//由上一个action中model.addAtrribute加入
step3==step3
step2==step2
step1==step1
mdafter==after//由上一个action中model.addAtrribute加入
aa==111 //aa由上一个action形参@ModelAttribute(“aa”)注入

name=mike// name由上一个action中modelAndView.addObject()加入

print  session parameter:
step4==step4

结论:

c.model,modelmap,map中put,request.setattribute(),b中@ModelAttribute以及modelandveiw.addObj()效果相同(可以自己测试),return时都是将参数放request的attribute中。

d.model,modelmap,map,modelandview的生命同期仅存在于当前方法内,forward/return后重新生成新空对象

打印方法代码

public class PrintRequestInfo {
public static void printSessionAndRequest(HttpServletRequest request, String remark) {
System.out.println("================" + remark + "====================");
System.out.println("print request parameter:");
final Enumeration reqEnum = request.getParameterNames();
while (reqEnum.hasMoreElements()) {
final String s = (String) reqEnum.nextElement();
System.out.println(s + "==" + request.getParameter(s));
} System.out.println("print request attribute:");
final Enumeration reqAttrs = request.getAttributeNames();
while (reqAttrs.hasMoreElements()) {
final String s = (String) reqAttrs.nextElement();
System.out.println(s + "==" + request.getAttribute(s));
} System.out.println("print session parameter:");
final HttpSession session = request.getSession();
final Enumeration se = session.getAttributeNames();
while (se.hasMoreElements()) {
final String key = (String) se.nextElement();
System.out.println(key + "==" + session.getAttribute(key));
}
}
}

4.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> </head> <body>
This is webSuc JSP page. <br>
${name}<br>
${aa}<br>
<c:out value="Hello World"></c:out>
${param.name}<br>
<%=request.getAttribute("name") %><br>
${aa} </body>
</html>

jsp页面结果:

This is webSuc JSP page.
mike
111
Hello World mike
111

springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系的更多相关文章

  1. springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

    springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...

  2. spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

    spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void.下面将对具体的一一进行说明: ModelAn ...

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

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

  4. SpringMVC的Model ModeMap ModelAndView @ModelAttribute @SessionAttribute区分

    Spring MVC整理系列(05)————Spring MVC配置解析及整合SpriSpring MVC之@ModelAttribute.@SessionAttributes以及Model的使用介绍 ...

  5. SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、

    1.处理模型数据 Spring MVC 提供了以下几种途径输出模型数据:      – ModelAndView:处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 ...

  6. SpringMVC Map Model ModelMap 和 ModelAndView

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

  7. SpringMVC 向页面传值-Map、Model和ModelMap

    除了使用ModelAndView方式外.还可以使用Map.Model和ModelMap来向前台页面传值 使用后面3种方式,都是在方法参数中,指定一个该类型的参数.例如: Java代码 @Request ...

  8. springMVC Model ModelMap 和 ModelAndView的区别(转)

    原文地址:springMVC Model ModelMap 和 ModelAndView的区别 近来在看代码,发现controller里有不同的处理返回数据的方式,而自己一直在用ModelAndVie ...

  9. ModelAndView存取数Model、Map、ModelMap

    1.从名字上看ModelAndView中的Model代表模型,View代表视图,这个名字就很好地解释了该类的作用.(对应mvc配置,寻找模型,返回视图.) 2.业务处理器调用模型层处理完用户请求后,把 ...

随机推荐

  1. 【git】之常用命令

    再使用git过程常用的命令在6-10左右,但是如果你想精通git那么需要记住的命令在80左右, 下面看这张图,我们了解一下git的概念的常用操作! Workspace:工作区(例如eclipse的工作 ...

  2. MySQL 服务启动y异常: 本地无法启动MySQL服务,报的错误:1067,进程意外终止---解决

    启动MySQL后,几秒钟后直接报错了 然后在事件查看器中发现了几条错误信息 Can't start server: Bind on TCP/IP port: No such file or direc ...

  3. 服务网关zuul之二:过滤器--请求过滤执行过程(源码分析)

    Zuul的核心是一系列的过滤器,这些过滤器可以完成以下功能: 身份认证与安全:识别每个资源的验证要求,并拒绝那些与要求不符的请求. 审查与监控:在边缘位置追踪有意义的数据和统计结果,从而带来精确的生成 ...

  4. [转][Centos]一、了解关机

    来自:https://blog.csdn.net/ronmy/article/details/79117390 Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1 ...

  5. [UE4]更新UI的三种方式

    一.函数绑定 二.属性绑定 只会列出匹配的数据类型. 三.事件驱动更新 啦啦啦啦啦 结论:函数和属性绑定的原理都是每帧都去调用绑定的函数/属性,效率比较低下,一般不推荐使用.事件驱动更新的效率最好,性 ...

  6. centos7 安装pip+python3.6

    centos7安装pip 1.执行:yum install python-pip 若没有python-pip包,先执行:yum -y install epel-release,再执行yum insta ...

  7. centos7 使用二进制安装mysql 5.7.23

    1.下载二进制安装包 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz cd /usr/local/src wget https://cdn.mysql.com// ...

  8. (转)开源OpenWRT知识

    原博地址:http://www.thinkingquest.net/article/466 我们都需要使用google提供的搜索,gmail等优质服务.但是由于方墙的存在,使得大家各自搞各自的FQ办法 ...

  9. 【TensorFlow学习笔记 】name_socpe variable_scope

    [引言]TensorFlow中的命名域是非常重要的概念,涉及到参数共享,方便命名参数管理,定义图结构 本文主要介绍name_scope 和 variable_scope,slim包中的arg_scop ...

  10. java入门简介

    1.java运行环境 下载的jdk中包含了java运行时的环境(JRE),JRE又包含了java虚拟机(JVM) 2.java运行过程 源文件(.java)由编译器编译为字节码(.class)文件,再 ...