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") ...
随机推荐
- JAVA图搜索算法之DFS-BFS
图算法DFS与BFS BFS和DFS代表对图进行遍历,即搜索的算法,搜索算法中常用的只要有两种算法:深度优先遍历(Depth-First-Search : DFS)和广度优先遍历(Breadth-Fi ...
- Centos7下创建centos-home逻辑分区
1备份要挂载的文件夹 查看home文件夹有无文件,如有文件一定要记得备份 2创建逻辑分区 2.1查看已有逻辑分区 2.2查看磁盘分区情况 2.3查看磁盘PV 2.4创建逻辑分区 lvcreate -n ...
- [ABC205E] White and Black Balls 题解
White and Black Balls 题目大意 将 \(n\) 个白球,\(m\) 个黑球排成一列,要求满足 \(\forall i\in[1,n+m],w_i\le b_i+k\),问存在多少 ...
- meet
以后就放弃csdn了,就来这里记录自己的成长,就当成一个树洞吧,开心与难过,学习与生活,进步与成长,留下时间的痕迹!冲!冲!冲!
- Radius+OpenLdap+USG防火墙认证
1.1.安装OpenLdap # 在数据目录创建ldap文件存放ldap的配置文件 mkdir -p /data/ldap/{data,conf} docker run -p 389:389 -p 6 ...
- 机器学习从入门到放弃:硬train一发手写数字识别
一.前言 前面我们了解了关于机器学习使用到的数学基础和内部原理,这一次就来动手使用 pytorch 来实现一个简单的神经网络工程,用来识别手写数字的项目.自己动手后会发现,框架里已经帮你实现了大部分的 ...
- .NET周刊【11月第1期 2023-11-09】
国内文章 C#/.NET/.NET Core优秀项目和框架2023年10月简报 https://www.cnblogs.com/Can-daydayup/p/17804085.html 本文主要介绍了 ...
- 记一次线上问题引发的对 Mysql 锁机制分析
背景 最近双十一开门红期间组内出现了一次因 Mysql 死锁导致的线上问题,当时从监控可以看到数据库活跃连接数飙升,导致应用层数据库连接池被打满,后续所有请求都因获取不到连接而失败 整体业务代码精简逻 ...
- (Good topic)字符串的最大公因子 (3.21leetcode每日打卡)
对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连接 1 次或多次)时,我们才认定 "T 能除尽 S". 返回最长字符串 X,要求满足 X 能除尽 s ...
- (Good topic)单词的压缩编码(leetcode3.28每日打卡)
给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell&quo ...