域对象共享数据

15 篇文章 1 订阅

目录

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

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

🦆ModuleAndView包含Module和View的功能

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

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

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

六、Model、ModelMap、Map的关系

七、向session域共享数据

八、向application域共享数据


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

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

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

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

🦆ModuleAndView包含Module和View的功能

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

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

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

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
    <body>
         <h1>success.html</h1>
         <p th:text="${testRequestScope}"></p>
    </body>
</html>
package com.atguigu.controller;

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

@Controller
public class TestScopeController {
    @RequestMapping("/test/mav")
    public ModelAndView testMAV(){
        ModelAndView mav = new ModelAndView();
        //向请求域中共享数据
        mav.addObject("testRequestScope","hello,ModuleAndView");
        //设置逻辑视图
        mav.setViewName("success");
        return mav;
    }
}

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

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

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

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

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

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

六、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 {}

七、向session域共享数据

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

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

八、向application域共享数据

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

elk-zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值