Geeks_Z の Blog Geeks_Z の Blog
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)

Geeks_Z

AI小学生
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)
  • Linux

  • 数据结构与算法

  • 算法设计与分析

  • Java

  • Python

  • 设计模式

  • 计算机网络

  • Spring笔记

  • SpringMVC笔记

    • SpringMVC简介
    • HelloWorld
    • RequestMapping注解
    • SpringMVC获取请求参数
    • 域对象共享数据
      • SpringMVC的视图
      • RESTful
      • HttpMessageConverter
      • 文件上传和下载
      • 拦截器
      • 异常处理器
      • 注解配置SpringMVC
      • SpringMVC执行流程
    • SpringBoot笔记

    • SpringSecurity

    • Elasticsearch笔记

    • RabbitMQ笔记

    • Docker笔记

    • MySQL

    • Redis

    • Mybatis

    • MybatisPlus

    • Nginx

    • Kubernetes笔记

    • Git

    • Software

    • 微服务笔记

    • bug

    • BackEndNotes
    • SpringMVC笔记
    Geeks_Z
    2023-01-15
    目录

    域对象共享数据

    五、域对象共享数据

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

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

    2、使用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

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

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

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

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

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

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

    6、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

    7、向session域共享数据

    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope", "hello,session");
        return "success";
    }
    
    1
    2
    3
    4
    5

    8、向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

    上次更新: 2024/03/29, 08:56:31
    SpringMVC获取请求参数
    SpringMVC的视图

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

    最近更新
    01
    并行训练
    03-29
    02
    tensor维度转换
    03-26
    03
    ResNet源码解读
    03-23
    更多文章>
    Theme by Vdoing | Copyright © 2022-2024 Geeks_Z | MIT License
    京公网安备 11010802040735号 | 京ICP备2022029989号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式