结论:

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. mike111Hello Worldmike111

转载于:https://www.cnblogs.com/pu20065226/p/10076758.html

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

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

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

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

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

  3. 前端接modelmap的list_SpringMVC - 数据怎么从后端到前端?Model, ModelMap, ModelAndView

    总结 SpringMVC在调用方法前会创建一个隐含的数据模型(Model,ModelMap),作为模型数据的存储容器, 成为"隐含模型". 如果controller方法的参数为Mo ...

  4. SpringMVC里的Model、Map、ModelMap以及ModelAndView

    ① Model是什么? SpringMVC内部使用一个org.springframework.ui.Model接口存储的数据模型,它的功能类似于java.uitl.Map,但是比Map更好用 org. ...

  5. Map+Model+ModelMap介绍

    Map+Model+ModelMap 接口:java.util.Map 接口:org.springframework.ui.Model 类:   org.springframework.ui.Mode ...

  6. 前端接modelmap的list_页面间传递前端请求参数和获取参数:Model model,HttpServletRequest request, ModelMap map参数使用与区别...

    Model model, HttpServletRequest request, ModelMap map声明变量 一.下面的方法是需要将请求发过来的数据(或者说参数)传递到重定向的页面/转发的页面的 ...

  7. Spring MVC中的Model, ModelMap, 和ModelAndView

    1. 概述 在本文中,我们将介绍由弹簧MVC提供的核心组织弹簧框架,组织弹簧框架.. 2. Maven依赖关系 让我们从pom.xml文件中的弹簧上下文依赖关系开始: <dependency&g ...

  8. GFSChubbyBigTable三者定义详解相互关系

    GFS&Chubby&BigTable三者定义详解&相互关系 一.GFS & Chubby & BigTable 定义 1.关于GFS 2.关于Chubby 3 ...

  9. 获取长度length_lab、labE、la、laE、ll、llE 钢筋锚固搭接长度6项参数的相互关系...

    文|施工小诸葛 目录 01   相关概念 02   字母含义 03   lab 非抗震纵向受拉钢筋的基本锚固长度 04   la 非抗震纵向受拉钢筋的锚固长度 05   ll 非抗震纵向受拉钢筋搭接长 ...

  10. Microbiome:应用多维宏组学方法协同揭示复杂细菌群落对目标底物代谢的菌间相互关系(一作解读)...

    Microbiome: 应用多维宏组学方法协同揭示复杂细菌群落对目标底物代谢的菌间相互关系 香港大学张彤教授团队与北京大学余珂博士研究团队,以生物降解菌群为研究模型,运用多维宏组学方法(宏基因组.宏转 ...

最新文章

  1. mysql右下角托盘中的图标_MFC下托盘图标的实现和托盘菜单。
  2. MySQL数据库:视图View
  3. SharedPreferences操作数据
  4. 几个年薪百万的下属,爆了~
  5. flask Flash消息
  6. Linux centosVMware xshell使用xftp传输文件、使用pure-ftpd搭建ftp服务
  7. comsol固体传热_参与介质中辐射传热的 4 种计算方法
  8. Go基础:产生随机数
  9. SpringCloud系列第09节之消息总线Bus
  10. 欧洲语言框架A1到C2,法语等级 A1、A2、B1、B2、C1、C2
  11. 组成计算机网络必备的条件是什么,要组成计算机网络必须具备的三要素
  12. 暗影精灵4如何调节风扇转速_惠普暗影精灵4 简单几步设置让游戏画面更流畅
  13. 解决go合约fabric shim peer依赖问题
  14. Maven的下载与配置 和在IDEA中创建Maven的项目
  15. C语言类型限定符(type specifier)(一)——volatile详细教程
  16. android path拆分_Android知识总结——Path常用方法解析
  17. mybatis中resultMap和resultType区别,三分钟读懂
  18. oracle常用日期格式,ORACLE常用日期数据格式
  19. oracle中 ''dual'' 的含义
  20. SSL加密与分布式IM系统-InChat1.1.3版本试用说明

热门文章

  1. centos7中firewall防火墙命令详解
  2. Docker 的使用
  3. php将excel日期转成时间戳,使用PHP将Excel日期编号转换为Unix时间戳时不匹配
  4. 【Java程序设计】接口与多态
  5. clickhouse修改表的TTL
  6. sbt命令行常用命令
  7. CASIA WebFace、WIDDER FACE、FDDB、AFLW、CelebA训练集详解
  8. nodejs中全栈开发框架meteor的文档
  9. 特殊权限及SUID,facl及Linux的终端
  10. MPLS ××× Carrier Supporting Carrier Option AB(一)