当前位置:网站首页>Domain objects share data
Domain objects share data
2022-08-07 14:05:00 【piggy.get】
目录
一、使用ServletAPI向request域对象共享数据(不常用)
二、使用ModelAndView向request域对象共享数据
一、使用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时,可以使用其ModuleFunctions share data with the requesting domain
使用ViewFunctional Settings Logical View,But the controller method must beModuleAndView作为方法的返回值
<!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、MapType parameters are essentially all BindingAwareModelMap 创建的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
七、向session域共享数据
Only if the browser is closed or not,It doesn't matter if the server is down or not
<a th:href="@{/test/session}">Tests share data to the session domain</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}">Tests share data with the app domain</a><br>@RequestMapping("/test/application")
public String testApplication(HttpSession session){
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("testSessionScope", "hello,application");
return "success";
}
边栏推荐
- eyb: Creation process: build the environment to display the menu (1)
- Error: required args <xml=string> at error (index.esm.js?93ce:68:1) at Parser.parse (index.e
- NestedTensor
- 09.有条件的渲染插槽
- top-k问题详解——通过堆解决高频面试题
- Controller Layer Coding Specification
- 5 Practical SQL Examples for Mastering SQL GROUP BY in SQL Tutorial (including complete SQL and test data)
- @RequestMapping注解标识的位置
- 下一代无线局域网-高吞吐率
- ZCMU--2126: Minute thoughts
猜你喜欢

LOGO 8.3 Web Server功能

内存管理(三)——内存分页

内存管理(五)——内存回收

Fiddler breakpoint graphic explanation
![[Leetcode]21. 合并两个有序链表](/img/86/e3a14a64dafece194e44aec91cea44.png)
[Leetcode]21. 合并两个有序链表

2022年危险化学品生产单位安全生产管理人员考试题模拟考试题库及答案

实战 || 某软件股份有限公司通用漏洞挖掘

CSO面对面|对话迷你世界,畅谈游戏行业的安全建设

Yangzhou University responded that "Dr. Tsinghua has been employed for 5 years, and the monthly salary is only 372 yuan"

2022年流动式起重机司机试题模拟考试平台操作
随机推荐
【vscode代码片段增加和删除】
扬州大学回应「清华博士入职5年,月薪才372元」
【YOLOv5】结合GradCAM热力图可视化
FairMOT Online Tracking Framework and Code Learning Records
Qt实现基于matchtemplate的长截图
Event registration: How to efficiently respond to the current real-time scene needs?
HJ4 字符串分隔
如何判断一个点在多边形内
USB Module Analysis (4) - Device List & Permission Application
OpenGL 光源分类 行为理论及实现
图文详解:如何给女朋友解释什么是微服务?
B端产品设计流程--产品定位
使用通达信软件买股票安全吗?
HJ3 obvious random number
Dart语言简介
Fiddler断点图文讲解
红队web打点信息收集
ZCMU--2126: Minute thoughts
LeetCode_6141_合并相似的物品
5 Practical SQL Examples for Mastering SQL GROUP BY in SQL Tutorial (including complete SQL and test data)