SpringMVC入门到实战------5、域对象共享数据 Request、Session、Application、Model、ModelAndView、Map、ModelMap的详细使用及代码实例

简介: 这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。

环境搭建:改系列文章开始有介绍。这部分代码所在磁盘位置:F:\workspace\SpringMVC代码\springMVC-demo4

从源码角度分析控制器:https://blog.csdn.net/weixin_43304253/article/details/128071577

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

设置: request.setAttribute("testRequestScope","Hello,ServletAPI");。取值:通过key

1.1 核心部分代码

设置值

    /**
     * 使用servletAPI向request域对象共享数据
     * @param request
     * @return
     */
    @RequestMapping("/testRequestScope")
    public String toSuccess(HttpServletRequest request){
        request.setAttribute("testRequestScope","Hello,ServletAPI");
        return "success";
    }
AI 代码解读

跳转后页面获取数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
<h1>这里是成功页面</h1>
<p th:text="${testRequestScope}"></p>
</body>
</html>
AI 代码解读

1.2 测试结果

在这里插入图片描述

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

1.1 核心代码

    /**
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        //用来处理模型数据,向请求域request共享数据
        mav.addObject("testRequestScope", "Hello,ModelAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;

    }
AI 代码解读

取值同1.1

1.2 测试结果

在这里插入图片描述

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

3.1 核心代码

    /**
     * 测试Model
     * @param model
     * @return
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","Hello,Model");
        return "success";
    }
AI 代码解读

取值同1.1

3.2 测试结果

在这里插入图片描述


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

4.1 核心代码

    /**
     * 测试MaP
     * @param map
     * @return
     */
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","Hello,Map");
        return "success";
    }![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/adf9064bab234ef095db886edac4a9cf.png)
AI 代码解读

取值方法同1.1

4.2 测试结果

在这里插入图片描述

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

5.1 核心代码

    /**
     * 测试 ModelMap
     * @param modelMap
     * @return
     */
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","Hello,ModelMap");
        return "success";
    }
AI 代码解读

取值同1.1

5.2 测试结果

在这里插入图片描述

6、Model、ModelMap、Map的关系

CTRL+H 查看实现接口类。通过反射查看调用的对应方法

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 {}
AI 代码解读

通过反射查看调用的方法

System.out.println(model.getClass().getName());
System.out.println(map.getClass().getName());
System.out.println(modelMap.getClass().getName());
AI 代码解读

在这里插入图片描述

在这里插入图片描述

7、向session域共享数据

7.1 核心代码

    /**
     * 测试Session
     * @param session
     * @return
     */
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSession","Hello Session");
        return "success";

    }
AI 代码解读

取值方法

<p th:text="${session.testSession}"></p>
AI 代码解读

7.2 测试结果

在这里插入图片描述

8、向application域共享数据

8.1 核心代码

    /**
     * 测试Application
     * @param session
     * @return
     */
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplication","Hello Application");
        return "success";
    }
AI 代码解读

获取数据

<p th:text="${application.testApplication}"></p>
AI 代码解读

8.2 测试结果

在这里插入图片描述

9、项目结构

9.1 结构

在这里插入图片描述

9.2 首页跳转链接

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<a th:href="@{/testRequestScope}">测试requestAPI</a>
<hr>
<a th:href="@{/testModelAndView}">测试ModelAndView</a>
<hr>
<a th:href="@{/testModel}">测试Model</a>
<hr>
<a th:href="@{/testMap}">测试Map</a>
<hr>
<a th:href="@{/testModelMap}">测试ModelMap</a>
<hr>
<a th:href="@{/testSession}">测试Session</a>
<hr>
<a th:href="@{/testApplication}">测试Application</a>
</body>
</html>
AI 代码解读

9.3 数据展示页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
<h1>这里是成功页面</h1>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSession}"></p>
<p th:text="${application.testApplication}"></p>
</body>
</html>
AI 代码解读

9.4 跳转控制器

package com.zyz.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Map;

/**
 * @author zyz
 * @version 1.0
 * @data 2022/11/27 20:58
 * @Description:
 */

@Controller
public class TestController {
    @RequestMapping("/")
    public String toIndex() {
        return "index";
    }

    /**
     * 使用servletAPI向request域对象共享数据
     *
     * @param request
     * @return
     */
    @RequestMapping("/testRequestScope")
    public String toSuccess(HttpServletRequest request) {
        request.setAttribute("testRequestScope", "Hello,ServletAPI");
        return "success";
    }

    /**
     * 测试ModelAndView
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图,实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        //用来处理模型数据,向请求域request共享数据
        mav.addObject("testRequestScope", "Hello,ModelAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;

    }

    /**
     * 测试Model
     * @param model
     * @return
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","Hello,Model");
        System.out.println(model.getClass().getName());
        return "success";
    }

    /**
     * 测试MaP
     * @param map
     * @return
     */
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","Hello,Map");
        System.out.println(map.getClass().getName());
        return "success";
    }

    /**
     * 测试 ModelMap
     * @param modelMap
     * @return
     */
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","Hello,ModelMap");
        System.out.println(modelMap.getClass().getName());
        return "success";
    }

    /**
     * 测试Session
     * @param session
     * @return
     */
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSession","Hello Session");
        return "success";

    }

    /**
     * 测试Application
     * @param session
     * @return
     */
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplication","Hello Application");
        return "success";
    }

}
AI 代码解读

10、后语

学无止境。。。。。。

目录
打赏
0
1
1
0
218
分享
相关文章
Java Map实战:用HashMap和TreeMap轻松解决复杂数据结构问题!
【10月更文挑战第17天】本文深入探讨了Java中HashMap和TreeMap两种Map类型的特性和应用场景。HashMap基于哈希表实现,支持高效的数据操作且允许键值为null;TreeMap基于红黑树实现,支持自然排序或自定义排序,确保元素有序。文章通过具体示例展示了两者的实战应用,帮助开发者根据实际需求选择合适的数据结构,提高开发效率。
139 2
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
这篇文章详细解释了在IntelliJ IDEA中如何使用Mute Breakpoints功能来快速跳过程序中的后续断点,并展示了如何一键清空所有设置的断点。
域对象共享数据model、modelAndView、map、mapModel、request。从源码角度分析
Request获取Map集合,提取username后面的值方法,post和get提取集合的方法
Request获取Map集合,提取username后面的值方法,post和get提取集合的方法
Java Map实战:用HashMap和TreeMap轻松解决复杂数据结构问题!
【6月更文挑战第18天】在Java中,HashMap基于哈希表提供快速的键值对操作,适合无序数据;而TreeMap利用红黑树保证排序,适用于有序场景。示例展示了HashMap如何存储并查找用户信息,以及TreeMap如何按员工编号排序存储员工名。两者在不同需求下优化了数据处理。
180 0
|
11月前
【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)
【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)
96 1
|
11天前
使用 entrySet 遍历 Map 类集合 KV
使用 entrySet 遍历 Map 类集合 KV
|
10月前
|
Dart之集合详解(List、Set、Map)
Dart之集合详解(List、Set、Map)
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等