• (续)SSM整合之springmvc笔记(域对象共享数据)(P136-138)


    目录

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

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

    1   新建TestScopeController

    2  index.html

    3  书写TestScopeController

    4  success.html

    5   测试 

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

    1  index.html

     2    TestScopeController

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

    1 . index.html

     2    TestScopeController

    3   测试

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

    1 . index.html

     2    TestScopeController

    3   测试

    六   Model、ModelMap、Map的关系

    七   向session域共享数据

    八   向application域共享数据

    1 . index.html

    2    TestScopeController

    3 .success.html

     4 测试


    一  使用ServletAPIrequest域对象共享数据

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

    二  使用ModelAndViewrequest域对象共享数据

    1   新建TestScopeController

    2  index.html

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

    3  书写TestScopeController

    1. /*
    2. * * 向域对象共享数据:
    3. * 1、通过ModelAndView向请求域共享数据
    4. * 使用ModelAndView时,可以使用其Model功能向请求域共享数据
    5. * 使用View功能设置逻辑视图,但是控制器方法一定要将ModelAndView作为方法的返回值
    6. * */
    7. @Controller
    8. public class TestScopeController {
    9. @RequestMapping("/test/mav")
    10. public ModelAndView testMAV(){
    11. /**
    12. * ModelAndView包含Model和View的功能
    13. * Model:向请求域中共享数据
    14. * View:设置逻辑视图实现页面跳转
    15. */
    16. ModelAndView mav = new ModelAndView();
    17. //向请求域中共享数据
    18. mav.addObject("testRequestScope", "hello,ModelAndView");
    19. //设置逻辑视图
    20. mav.setViewName("success");
    21. return mav;
    22. }
    23. }

    4  success.html

    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>

    5   测试 

     

     总结 :

      向域对象共享数据:
      1、通过ModelAndView向请求域共享数据
       使用ModelAndView时,可以使用其Model功能向请求域共享数据
       使用View功能设置逻辑视图,但是控制器方法一定要将ModelAndView作为方法的返回值
    

    三  使用Modelrequest域对象共享数据

    1  index.html

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

     2    TestScopeController

    1. /* 2、使用Model向请求域共享数据 */
    2. @RequestMapping("/test/model")
    3. public String testModel(Model model){
    4. //org.springframework.validation.support.BindingAwareModelMap
    5. System.out.println(model.getClass().getName());
    6. model.addAttribute("testRequestScope", "hello,Model");
    7. return "success";
    8. }

    3    测试

     

    四  使用maprequest域对象共享数据

    1 . index.html

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

     2    TestScopeController

    1. /*4、使用map向请求域共享数据*/
    2. @RequestMapping("/test/map")
    3. public String testMap(Map map){
    4. //org.springframework.validation.support.BindingAwareModelMap
    5. System.out.println(map.getClass().getName());
    6. map.put("testRequestScope", "hello,map");
    7. return "success";
    8. }

    3   测试

    五  使用ModelMaprequest域对象共享数据

    1 . index.html

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

     2    TestScopeController

    1. /*3、使用ModelMap向请求域共享数据*/
    2. @RequestMapping("/test/modelMap")
    3. public String testModelMap(ModelMap modelMap){
    4. //org.springframework.validation.support.BindingAwareModelMap
    5. System.out.println(modelMap.getClass().getName());
    6. modelMap.addAttribute("testRequestScope", "hello,ModelMap");
    7. return "success";
    8. }

    3   测试

    六   ModelModelMapMap的关系

    ModelModelMapMap类型的参数其实本质上都是 BindingAwareModelMap 类型的

    输入类型

    System.out.println(modelMap.getClass().getName());

    System.out.println(map.getClass().getName());

    System.out.println(model.getClass().getName());

     

     查看源码BindingAwareModelMap

    按二次Shift

    BindingAwareModelMap extends ExtendedModelMap

    Ctrl+左健

    ExtendedModelMap extends ModelMap

    ModelMap extends LinkedHashMap

    LinkedHashMap  extends HashMap

    总结:

    5、Model和ModelMap和map的关系
    * 其实在底层中,这些类型的形参最终都是通过BindingAwareModelMap创建
    * public class BindingAwareModelMap extends ExtendedModelMap {}
    * public class ExtendedModelMap extends ModelMap implements Model {}
    * public class ModelMap extends LinkedHashMap {}

    七   向session域共享数据

    八   向application域共享数据

    1 . index.html

    1. <a th:href="@{/test/session}">测试向会话域共享数据a><br>
    2. <a th:href="@{/test/application}">测试向应用域共享数据a><br>

    2    TestScopeController

    1. @RequestMapping("/test/session")
    2. public String testSession(HttpSession session){
    3. session.setAttribute("testSessionScope", "hello,session");
    4. return "success";
    5. }
    6. @RequestMapping("/test/application")
    7. public String testApplication(HttpSession session){
    8. ServletContext servletContext = session.getServletContext();
    9. servletContext.setAttribute("testApplicationScope", "hello,application");
    10. return "success";
    11. }

    3 .success.html

    1. <p th:text="${session.testSessionScope}">p>
    2. <p th:text="${application.testApplicationScope}">p>

     4 测试

    现在我们先把网址复制一下 然后 关闭浏览器

    在打开浏览器 粘贴上刚才复制的网址 

    http://localhost:8080/springMVC/test/application

    会现在这时session的数据没有了  只有application  因为application还没有关闭  

     

     现在我们从新部暑   然后在往seiion中共享一个数据

    现在在重新重启服务器  重新部暑

  • 相关阅读:
    重温 C# 字典Dictionary类
    vue2 ElementUI 表单标签、表格表头添加问号图标提示
    STM32——NVIC中断优先级管理分析
    html5兼容性处理(PC浏览器)
    用unity和c#实现
    生成式AI - 大模型(LLM)提示工程(Prompt)技巧
    从React源码来学hooks是不是更香呢
    基于FreeBSD 8.0 Ports配置nginx+php+mysql高性能web平台【解决方案】
    java计算机毕业设计ssm+vue社区公益服务平台
    小程序商城运营如何做好用户定位和用户需求
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127914322