我们知道,四大域对象包括:pageContext、request、session和application,而pageContext有效范围为一个jsp页面,太小了,而application有效范围为整个web工程,又太大了。因此我们平时的开发中在域对象中共享数据主要是用request和session。

我们来搭建本博客的环境
首先,创建一个 web 模块并配置好web.xml和部署Tomcat
其次,在webapp/WEB-INF/templates/下创建index.html和target.html(名字任取)文件。
然后,编写跳转到首页index.html的请求控制器controller代码:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class IndexController {@RequestMapping("/")public String testRequestMapping(){return "index";}
}

一、向 request 域对象共享数据

(一)使用ServletAPI向request域对象共享数据

案例

controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;@Controller
public class MyController {@RequestMapping(value = "/testRequestByServletAPI")public String testRequestByServletAPI(HttpServletRequest request){request.setAttribute("testRequestScope","hello servletAPI");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据</a>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

运行Tomcat:

(二)使用ModelAndView向request域对象共享数据(推荐)

案例
controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
public class MyController {@RequestMapping(value = "/testModelAndView")public ModelAndView testModelAndView(){ModelAndView mav = new ModelAndView();//处理模型数据,即向请求域request共享数据mav.addObject("testRequestScope","hello ModelAndView");//设置视图名称mav.setViewName("target");return mav;}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

(三)使用Model向request域对象共享数据

案例
controller:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class MyController {@RequestMapping(value = "/testModel")public String testModel(Model model){model.addAttribute("testRequestScope","hello model");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

(四)使用map向request域对象共享数据

案例
controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;@Controller
public class MyController {@RequestMapping(value = "/testMap")public String testMap(Map<String,Object> map){//请求域中的内容各种各样,因此是Objectmap.put("testRequestScope","hello map");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

(五)使用ModelMap向request域对象共享数据

案例
controller:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class MyController {@RequestMapping(value = "/testModelMap")public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testRequestScope","hello ModelMap");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${testRequestScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

(六)Model、ModelMap、Map的关系

案例
假如我们把上面三个例子都写到一块儿,并且在控制器方法的最后输出方法参数对应实例化对象的名字:
controller:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;@Controller
public class MyController {@RequestMapping(value = "/testModel")public String testModel(Model model){model.addAttribute("testRequestScope","hello model");System.out.println(model.getClass().getName());return "target";}@RequestMapping(value = "/testMap")public String testMap(Map<String,Object> map){//请求域中的内容各种各样,因此是Objectmap.put("testRequestScope","hello map");System.out.println(map.getClass().getName());return "target";}@RequestMapping(value = "/testModelMap")public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testRequestScope","hello ModelMap");System.out.println(modelMap.getClass().getName());return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a>
</body>
</html>

启动Tomcat:


三个链接都点一遍,观察IDEA输出:

我们发现,Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的。
而且我们通过以下的继承关系中可以看出BindingAwareModelMap是能够实例化Model、ModelMap、Map类型的。

public interface Model{}
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

二、向 session 域对象共享数据

案例
controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;@Controller
public class MyController {@RequestMapping(value = "/testSession")public String testSession(HttpSession session){session.setAttribute("testSessionScope","hello session");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testSession}">向session域对象共享数据</a><br>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${session.testSessionScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

三、向 application 域对象共享数据

案例

controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;@Controller
public class MyController {@RequestMapping(value = "/testApplication")public String testApplication(HttpSession session){ServletContext application = session.getServletContext();application.setAttribute("testApplicationScope","hello application");return "target";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--这个是thymeleaf命名空间-->
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
这是index.html<br>
<a th:href="@{/testApplication}">向application域对象共享数据</a><br>
</body>
</html>

target.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
这是target.html<br>
<p th:text="${application.testApplicationScope}"></p>
<!--这是thymeleaf的语法,访问request域中数据只需要写attribute的名字,
而如果是session域,就要写session.名字;
如果是application域,就要写application.名字-->
</body>
</html>

启动Tomcat:

SpringMVC学习总结(四)使用ModelAndView、Model、Map、ModelMap向request域对象共享数据/向session、application域对象共享数据相关推荐

  1. Charles学习(四)之使用Map local代理本地静态资源以及配置移动端代理在真机上调试iOS和Android客户端...

    前言 问题一:我们在App内嵌H5开发的过程中,肯定会遇到一个问题就是我不想在chrome的控制台中调试也不想在模拟器中调试,我想要在真机上调试,那么如何解决这个问题呢? 问题二:我们期待调试时达到的 ...

  2. SpringMVC学习笔记四:数据绑定

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831344.html  参考:http://www.cnblogs.com/HD/p/4107674.html ...

  3. SpringMVC学习(四)——Spring使用到的设计模式分析

    文章目录 1.引言 2.Spring常用的设计模式(先留坑,慢慢补充) 2.1 简单工厂模式 2.2 工厂方法模式 2.3 适配器模式 2.4 装饰器模式(包装器模式) 2.5 代理模式 2.6 观察 ...

  4. SpringMVC学习:控制层(Controller)基于注解详解

    文章目录 一.URL映射Controller的方法返回值 二.SpringMVC各类注解详解 (一) @Controller (二) @RequestMapping 1.基本用法 2. path属性或 ...

  5. Spring中Model、ModelMap、ModelAndView理解和具体使用总结

    在了解这三者之前,需要知道一点:SpringMVC在调用方法前会创建一个隐含的数据模型,作为模型数据的存储容器, 成为"隐含模型". 也就是说在每一次的前后台请求的时候会随带这一个 ...

  6. java model类作用_SPRING框架中ModelAndView、Model、ModelMap区别及详细分析

    注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面. 首先介绍ModelMap[Model]和ModelAndView的作用 Model 是一个接口, 其实现类为Exte ...

  7. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解...

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema ...

  8. SpringMVC的Model、Model Map、ModelAndView

    一直都在用Model设置属性用于前后端传值. 今天在使用@RestController时,使用了ModelAndView传值并返回试图. 1.Model model一般用于前后端传值. model不能 ...

  9. JavaEE 企业级分布式高级架构师(四)SpringMVC学习笔记(4)

    SpringMVC学习笔记 高级应用篇 ControllerAdvice @ControllerAdvice @ModelAttribute 作用于方法 作用于方法参数 @InitBinder @Ex ...

最新文章

  1. [Spoj]Counting Divisors (cube)
  2. 用C#开发.NET CF 蓝牙通信模块
  3. python不支持_为什么 Python 不支持函数重载?而其他语言大都支持?
  4. 【已解决】navigateTo:fail page “/pages/.../...“ is not found
  5. 三个点拟合圆形的函数C#
  6. 全球首发免费的MySql for Entity Framework Core
  7. 使用kubectl访问Kubernetes集群时的身份验证和授权
  8. NSAssert与assert断言
  9. whereis命令详解
  10. 怎样写毕业论文的开题报告和任务书?
  11. 《电子元器件的可靠性》——3.6节恒定应力加速寿命试验
  12. 【数字信号处理】FIR 滤波器基础理论
  13. 简历中尽量不要出现精通_在个人简历中,熟悉、了解、熟练、精通之间有什么区别?...
  14. mysql压缩修复数据库_压缩修复Access数据库
  15. Shiro源码分析(二)——获取Subject
  16. 用计算机制作演示文稿教案博客,信息技术:《制作演示文稿的一般过程》教案...
  17. Elasticsearch汉字补全和拼写纠错
  18. 一只青蛙一次可以跳上1级台阶,也可以跳上2级,也可以跳n级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)
  19. windows电脑防火墙关闭,一键系统防火墙关闭工具推荐
  20. Unity动画系统知识体系概览

热门文章

  1. script、iframe注入型防运营商劫持
  2. android move事件,Android的浏览器不处理touchmove事件正确
  3. Android ImageView 详解
  4. 画质增强概述-4-传统方法增强实践
  5. 【说透中台】01 | 来龙去脉:中台为什么这么火?
  6. CoreAnimation6-基于定时器的动画和性能调优
  7. css2.0圆角,CSS圆角效果-CSS3常用属性集合
  8. 一个免费兑换iPhone X的机会,就这样被你错过了
  9. 用html和css写一个动态的圣诞节贺卡
  10. 51nod1522上下序列