• 域对象共享数据


    目录

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

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

    🦆ModuleAndView包含Module和View的功能

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

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

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

    六、Model、ModelMap、Map的关系

    七、向session域共享数据

    八、向application域共享数据


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

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

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

    <a th:href="@{/test/mav}">测试通过ModelAndView向请求域共享数据a>

    🦆ModuleAndView包含Module和View的功能

    • Module:向请求域中共享数据
    • View:设计逻辑视图,实现页面跳转

    使用ModuleAndView时,可以使用其Module功能向请求域共享数据

    使用View功能设置逻辑视图,但是控制器方法一定要将ModuleAndView作为方法的返回值

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>success.htmlh1>
    9. <p th:text="${testRequestScope}">p>
    10. body>
    11. html>
    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.servlet.ModelAndView;
    5. @Controller
    6. public class TestScopeController {
    7. @RequestMapping("/test/mav")
    8. public ModelAndView testMAV(){
    9. ModelAndView mav = new ModelAndView();
    10. //向请求域中共享数据
    11. mav.addObject("testRequestScope","hello,ModuleAndView");
    12. //设置逻辑视图
    13. mav.setViewName("success");
    14. return mav;
    15. }
    16. }

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

    <a th:href="@{/test/model}">测试通过Model向请求域共享数据a>
    1. @RequestMapping("/test/model")
    2. public String testModel(Model model){
    3. model.addAttribute("testRequestScope","hello,Model");
    4. return "success";
    5. }

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

    <a th:href="@{/test/map}">测试通过Map向请求域共享数据a><br>
    1. @RequestMapping("/test/map")
    2. public String testMap(Map map){
    3. map.put("testRequestScope","hello,Map");
    4. return "success";
    5. }

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

    <a th:href="@{/test/modelMap}">测试通过ModelMap向请求域共享数据a><br>
    1. @RequestMapping("/test/modelMap")
    2. public String testModelMap(ModelMap modelMap){
    3. modelMap.addAttribute("testRequestScope","hello,ModelMap");
    4. return "success";
    5. }

    六、Model、ModelMap、Map的关系

    Model、ModelMap、Map类型的形参其实本质上都是 BindingAwareModelMap 创建的

    1. public interface Model{}
    2. public class ModelMap extends LinkedHashMap {}
    3. public class ExtendedModelMap extends ModelMap implements Model {}
    4. public class BindingAwareModelMap extends ExtendedModelMap {}

    七、向session域共享数据

    只与浏览器是否关闭有关,与服务器是否关闭无关

    <a th:href="@{/test/session}">测试向会话域共享数据a><br>
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>success.htmlh1>
    9. <p th:text="${testRequestScope}">p>
    10. <p th:text="${session.testSessionScope}">p>
    11. <p th:text="${application.testApplicationScope}">p>
    12. body>
    13. html>
    1. @RequestMapping("/test/session")
    2. public String testSession(HttpSession session){
    3. session.setAttribute("testSessionScope","hello,session");
    4. return "success";
    5. }

    八、向application域共享数据

    <a th:href="@{/test/application}">测试向应用域共享数据a><br>
    1. @RequestMapping("/test/application")
    2. public String testApplication(HttpSession session){
    3. ServletContext servletContext = session.getServletContext();
    4. servletContext.setAttribute("testSessionScope", "hello,application");
    5. return "success";
    6. }

     

  • 相关阅读:
    实体解析实施的复杂性
    永磁同步电机(PMSM)磁场定向控制(FOC)及Matlab/Simulink仿真分析
    搭建私有云盘 cloudreve
    JAVA技能树-打卡
    敏捷实践——沟通的重要性
    Java 的异常体系
    Docker之nacos集群部署(详细教你搭建)
    蓝蓝设计提供科研信息软件UI设计
    电脑安装双系统-linux系统上安装windows系统
    【FFH】啃论文俱乐部---世界上最快的C语言JSON库
  • 原文地址:https://blog.csdn.net/m0_52982868/article/details/126157542