一、使用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 系列:域对象共享数据的更多相关文章

  1. 【SpringMVC】域对象共享数据

    使用ModelAndView向request域对象共享数据 index.html <a th:href="@{/testModelAndView}">使用ModelAn ...

  2. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...

  3. 【Spring MVC系列】--(4)返回JSON

    [Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...

  4. 【ASP.NET MVC系列】浅谈数据注解和验证

    [ASP.NET MVC系列]浅谈数据注解和验证   [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google C ...

  5. Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验

    Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...

  6. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  7. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  8. spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)

    spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象) 它的出现原因: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用 ...

  9. Spring MVC中Controller如何将数据返回给页面

    要实现Controller返回数据给页面,Spring MVC 提供了以下几种途径: ModelAndView:将视图和数据封装成ModelAndView对象,作为方法的返回值,数据最终会存到Http ...

  10. hibernate+spring mvc, 解决hibernate 对象懒加载 json序列化问题

    引用地址 在使用Spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例: @RequestMapping("login") ...

随机推荐

  1. Termius for macOS or Windows Download ssh

    mac电脑   苹果系统 window 系统 链接  服务器,访问 ecs ,ssh链接 工具 下载地址:http://pgyd.online/?website/53.html Termius for ...

  2. Linux 中如何安全地抹去磁盘数据?

    哈喽大家好,我是咸鱼 离过职的小伙伴都知道,离职的时候需要上交公司电脑,但是电脑里面有许多我们的个人信息(聊天记录.浏览记录等等) 所以我们就需要先把这些信息都删除,确保无法恢复之后才上交 即有些情况 ...

  3. 掌握Go类型内嵌:设计模式与架构的新视角

    本文深入探讨了Go语言中的类型内嵌特性,从基础概念到实际应用,以及相关的最佳实践.文章不仅讲解了如何在Go中实现和使用类型内嵌,还通过具体的代码示例展示了其应用场景和潜在陷阱.最后,文章总结了类型内嵌 ...

  4. 【京东开源项目】微前端框架MicroApp 1.0正式发布

    介绍 MicroApp是由京东前端团队推出的一款微前端框架,它从组件化的思维,基于类WebComponent进行微前端的渲染,旨在降低上手难度.提升工作效率.MicroApp无关技术栈,也不和业务绑定 ...

  5. 谈谈selenium4.0中的相对定位

    相对定位历史 2021-10-13 发布的 selenium 4.0 开始引入,selenium 3.X是没有的 implement relative locator for find_element ...

  6. ubuntu 20.1 (linux) 下软件安装教程(基本上都是使用命令行安装)

    一.node 1. 安装教程 # 第二步,添加源后安装 需要什么版本直接替换后面的数字即可,如果需要21,直接将20替换为21就可以了 curl -sL https://deb.nodesource. ...

  7. 鬼谷子的钱袋(lgP2320)

    主要思路:二进制拆分. 先将 \(m\) 进行二进制拆分. 注意金币总数有限,也就是说拆分后可能会多出来一组.多出来的这组如果不是 \(2^n\) 就不需要考虑了,因为不会和前面的重复. 接下来考虑重 ...

  8. 我与Vue.js 2.x 的七年之痒

    --过去日子的回顾(这是个副标题) --其实这是篇广告软文(这是个副副标题) 以下是一些牢骚和感悟,不感兴趣的可以滑倒最下面,嘻嘻. 每每回忆起从前,就感觉时间飞逝,真切的感受到了那种课本中描述的白驹 ...

  9. mybatis plus很好,但是我被它坑了!

    作者今天在开发一个后台发送消息的功能时,由于需要给多个用户发送消息,于是使用了 mybatis plus 提供的 saveBatch() 方法,在测试环境测试通过上预发布后,测试反应发送消息接口很慢得 ...

  10. 解决报错Invalid bound statement (not found)

    解决报错Invalid bound statement (not found) 问题描述: 在玩mybatis-plus的时候,在测试类写了一个测试批量插入的方法,结果就报错: 它的意思是 无效的跳转 ...