当前位置:网站首页>域对象共享数据
域对象共享数据
2022-08-07 13:55:00 【小猪.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时,可以使用其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";
}
边栏推荐
- Introduction to Dart Language
- helm installation error Request entity too large: limit is 3145728
- excel sumifs多条件求和
- FairMOT Online Tracking Framework and Code Learning Records
- Yangzhou University responded that "Dr. Tsinghua has been employed for 5 years, and the monthly salary is only 372 yuan"
- 使用vxe-table行合并后所合并到的行后面会多出一列空白出来,
- Rust is one of the common problems: Blocking waiting for file lock on package cache
- 7. CuBLAS development in the Chinese version - cuBLAS guide cublasSetVector () and cublasGetVector ()
- 8. cuBLAS Development Guide Chinese version--cublasGetMatrix() and cublasSetMatrix() in cuBLAS
- OpenGL 光源分类 行为理论及实现
猜你喜欢

扬州大学回应「清华博士入职5年,月薪才372元」

08.v-for中遍历数据的几种方式

内存管理(一)——内存分配
![[Unity entry plan] GameObject class (1) - GetComponent method](/img/3b/2063744cae696f6ef95bcd3fb54135.png)
[Unity entry plan] GameObject class (1) - GetComponent method

Navicat连接mysql一段时间未操作之后卡住几秒

rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp)

How simple implement drag-and-drop ordering function menu

mysql 主从复制部署

使用cephadm部署单节点ceph集群,后期可扩容(基于官方文档,靠谱,读起来舒服)

室内定位之CSI指纹定位
随机推荐
LOGO 8.3 Web Server function
1.0 Common commands and permission understanding
Realize database addition, deletion, modification and query in one page
OneNote tutorial, how to use drawing and annotation in OneNote?
内存管理(三)——内存分页
若依使用EasyExcel导入和导出数据
excel sumifs多条件求和
Configure code snippets in Visual Studio Code to easily generate code templates with shortcut keys
Distributed pre-course: Redis implements distributed locks
在一个页面实现数据库的增删改查
2022/8/6
SQL教程之 掌握 SQL GROUP BY 的 5 个实用 SQL 示例(含完整sql与测试数据)
Introduction to Dart Language
图文详解:如何给女朋友解释什么是微服务?
2022/8/5 Topological sort+dp
Qt implementation based on matchtemplate long shots
How simple implement drag-and-drop ordering function menu
PDF conversion tool installation tutorial
Navicat gets stuck for a few seconds after connecting to mysql for a period of time
ZCMU--2126: 分分钟的碎碎念