EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • Spring5-基础

    • Spring5 入门案例
    • Spring核心之控制反转IOC
    • Spring核心之面向切面编程AOP
    • Jdbc Template
    • Spring5 事务
    • Spring5 新功能
    • Spring5 Webflux
  • SpringMVC-基础

    • SpringMVC 入门案例
    • RequestMapping注解
    • SpringMVC 获取请求参数
    • 域对象共享数据
      • 使用ServletAPI向request域对象共享数据
      • 使用ModelAndView向request域对象共享数据
      • 使用Model向request域对象共享数据
      • 使用map向request域对象共享数据
      • 使用ModelMap向request域对象共享数据
      • Model、ModelMap、Map的关系
      • 向session域共享数据
      • 向application域共享数据
    • SpringMVC的视图
    • RESTFul
    • RESTful案例
    • HttpMessageConverter
    • 文件上传和下载
    • 拦截器
    • 异常处理器
    • 注解配置SpringMVC
    • SpringMVC执行流程
  • SpringBoot2-基础

    • Spring与SpringBoot
    • SpringBoot2 快速入门
    • SpringBoot2 常用注解
    • 了解自动装配原理
  • SpringBoot2-核心功能

    • 配置文件
    • Web开发
    • 数据访问
    • 单元测试
  • SpringCloud-微服务架构

    • 微服务架构理论入门
    • SpringCloud 初步构建
    • Eureka 服务注册与发现
    • ZooKeeper 服务注册与发现
    • Consul 服务注册与发现
    • Ribbon 负载均衡服务调用
    • OpenFeign 简化服务调用
    • Hystrix 服务降级|熔断
    • GateWay 服务网关
    • Config 服务配置中心 与 BUS 消息总线
    • Stream 消息驱动
    • Sleuth 分布式请求链路追踪
  • SpringCloud Alibaba

    • SpringCloud Alibaba 简介
    • Nacos 服务发现、配置管理和服务管理平台
    • Sentinel 实现熔断与限流
    • Seata 分布式事务
  • Spring Security

    • SpringSecurity 概述
    • SpringSecurity 入门案例
    • SpringSecurity 登录认证详解
    • SpringSecurity 授权
    • SpringSecurity 跨域
    • SpringSecurity 遗留小问题
  • studynotes
  • Spring
  • SpringMVC
wufan
2022-01-18
目录

域对象共享数据

# 域对象共享数据

# 使用ServletAPI向request域对象共享数据

@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
    request.setAttribute("testScope", "hello,servletAPI");
    return "success";
}
1
2
3
4
5

# 使用ModelAndView向request域对象共享数据

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
    /**
     * ModelAndView有Model和View的功能
     * Model主要用于向请求域共享数据
     * View主要用于设置视图,实现页面跳转
     */
    ModelAndView mav = new ModelAndView();
    //向请求域共享数据
    mav.addObject("testScope", "hello,ModelAndView");
    //设置视图,实现页面跳转
    mav.setViewName("success");
    return mav;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 使用Model向request域对象共享数据

@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testScope", "hello,Model");
    return "success";
}
1
2
3
4
5

# 使用map向request域对象共享数据

@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
    map.put("testScope", "hello,Map");
    return "success";
}
1
2
3
4
5

# 使用ModelMap向request域对象共享数据

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "hello,ModelMap");
    return "success";
}
1
2
3
4
5

# Model、ModelMap、Map的关系

Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型*的

public interface Model{} //接口
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}//本质
1
2
3
4

# 向session域共享数据

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
	ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "hello,application");
    return "success";
}
1
2
3
4
5
6

# 向application域共享数据

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
	ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "hello,application");
    return "success";
}
1
2
3
4
5
6
Click to view the code writen by author
/**
 * @author frx
 * @version 1.0
 * @date 2022/1/18  13:45
 */
@Controller
public class TestController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * @author frx
 * @version 1.0
 * @date 2022/1/18  14:42
 */
@Controller
public class ScopeController {

    //使用servletAPI向request域对象共享数据
    @RequestMapping("/testRequestByservletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope","helloServletAPI");
        return "success";
    }
    @RequestMapping("/testModelAndView")
    public ModelAndView testModeAndView(){
        ModelAndView mav = new ModelAndView();
        //处理模型数据,即向请求域request共享数据
        mav.addObject("testRequestScope","hello.ModelAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;
    }

    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,model");
        return "success";
    }

    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","hello,map");
        return "success";

    }

    @RequestMapping("/testModelMap")
    public String testModeMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","hello,ModelMap");
        return "success";


    }
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope","Hello,Session");

        return "success";
    }

    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testSessionScope","Hello,application");
        return "success";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
success<br>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSessionScope}"></p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestByservletAPI}">通过servletAPI向request域对象共享数据</a><br>
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a><br>
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a><br>
<a th:href="@{/testSession}">通过ServletAPI向Session域对象共享数据</a><br>
<a th:href="@{/testApplication}">通过Application向Session域对象共享数据</a><br>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#SpringMVC
上次更新: 2024/04/21, 09:42:22
SpringMVC 获取请求参数
SpringMVC的视图

← SpringMVC 获取请求参数 SpringMVC的视图→

最近更新
01
微信支付功能的实现与流程
11-21
02
购物车与结算区域的深入优化与功能完善
11-21
03
购物车与结算区域的功能实现与优化
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式