spring-mvc 系列:域对象共享数据
一、使用ServletAPI向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
}
二、使用ModelAndView向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
}
三、使用Model向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
}
四、使用Map向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
}
五、使用ModelMap向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "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域共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
<a th:href="@{/testSession}">测试Session--->/testSession</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,Session");
return "success";
}
}
八、向application域共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
<a th:href="@{/testSession}">测试Session--->/testSession</a>
<a th:href="@{/testApplication}">测试Application--->/testApplication</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,Session");
return "success";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("testApplicationScope","hello,application");
return "success";
}
}
spring-mvc 系列:域对象共享数据的更多相关文章
- 【SpringMVC】域对象共享数据
使用ModelAndView向request域对象共享数据 index.html <a th:href="@{/testModelAndView}">使用ModelAn ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- 【Spring MVC系列】--(4)返回JSON
[Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...
- 【ASP.NET MVC系列】浅谈数据注解和验证
[ASP.NET MVC系列]浅谈数据注解和验证 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google C ...
- Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验
Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Spring mvc系列一之 Spring mvc简单配置
Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...
- spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)
spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象) 它的出现原因: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用 ...
- Spring MVC中Controller如何将数据返回给页面
要实现Controller返回数据给页面,Spring MVC 提供了以下几种途径: ModelAndView:将视图和数据封装成ModelAndView对象,作为方法的返回值,数据最终会存到Http ...
- hibernate+spring mvc, 解决hibernate 对象懒加载 json序列化问题
引用地址 在使用Spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例: @RequestMapping("login") ...
随机推荐
- Appendix D. Gradle Command Line
http://www.gradle.org/docs/current/userguide/gradle_command_line.html Appendix D. Gradle Command Lin ...
- .NET 8 RC 2 发布,将在11月14日发布正式版
微软2023-10-10 发布了 .NET 8 RC 2,下一站是.NET 8正式发布,就在下个月Net Conf 2023[1](11月14日)期间正式发布,我们也开始筹备第四届中国.NET开发者峰 ...
- 软件开发人员 Kubernetes 入门指南|Part 2
在第 1 部分中,我们讲解了 Kubernetes 的核心组件,Kubernetes 是一种开源容器编排器,用于在分布式环境中部署和扩展应用程序:我们还讲解了如何在集群中部署一个简单的应用程序,然后更 ...
- 从输入URL到页面加载完都发生了什么
1.浏览器的地址栏输入URL并按下回车. 2.浏览器查找当前URL是否存在缓存,并比较缓存是否过期. 3.DNS解析URL对应的IP. 4.根据IP建立TCP连接(三次握手). 5.HTTP发起请求. ...
- jenkins实践篇(1)——基于分支的自动发布
问题背景 想起初来公司时,我们还是在发布机上直接执行发布脚本来运行和部署服务,并且正式环境和测试环境的脚本都在一起,直接手动操作脚本时存在比较大的风险就是将环境部署错误,并且当时脚本部署逻辑还没有检测 ...
- powershell 7 初体验
支持枚举定义,类定义\类继承\接口继承,不支持接口定义\泛型类定义\泛型函数定义,但是作为shell脚本已经相当让人惊喜了, 基本逻辑可以直接套用C#语法格式 # enum_sample/main.p ...
- Python 潮流周刊#24:no-GIL 提案正式被采纳了!
你好,我是猫哥.这里每周分享优质的 Python.AI 及通用技术内容,大部分为英文.标题取自其中两则分享,不代表全部内容都是该主题,特此声明. 微信 | 博客 | 邮件 | Github | Tel ...
- Web Woeker和Shared Worker的使用以及案例
目录 1.前言 2.介绍 Web Worker 3.使用须知及兼容性 3.1.使用须知 3.2.兼容性 4.使用 Web Worker 4.1.创建 Web Worker 4.2.与主线程通信 4.3 ...
- offscreenCanvas+worker+IndexedDB实现无感大量图片缓存
一个有必要实现的需求 因为项目中需要使用canvasTexture(一个threejs3d引擎中的材质类型),绘制大量的图片,每次使用都会请求大量的oss图片资源,虽然重复请求会有磁盘缓存但毕竟这个磁 ...
- A-B数对 (hash映射)
题目大意: 第一行输入N,C 第二行输入n个数字 输出,求A - B = C的数对个数 样例 4 1 1 1 2 3 输出 3 思路:用STL容器map,map<num, times>,建 ...