一、使用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. Python来源介绍

    python来源 1.1 Python来源 1989年的圣诞节,一位来自荷兰,名叫Guidio van Rossum的年轻帅小伙子,为了打发无趣的时光,决定改善他参与设计,不是很满意的ABC语言,随着 ...

  2. July 2023 (version 1.81)

    July 2023 (version 1.81) 更新后显示发行说明 Update 1.81.1: The update addresses these issues. Welcome to the ...

  3. CF755C

    题目简化和分析: 这题不用说怎么分析了吧,这一看就是个并查集求连通分量个数的经典模板. 我们需要将 \(i\) 和 \(p_i\) 进行合并. 遍历每个 \(i\) 与 \(i+1\) 是否属于同一个 ...

  4. Arithmetic Progression 题解

    Arithmetic Progression 题目大意 存在一个打乱了顺序的等差数列 \(a\),你可以询问不超过 \(60\) 次,每次可以以以下两种方式之一进行询问: 查询 \(a\) 中是否有严 ...

  5. Python:发送邮件或发带附件的邮件

    在进行发邮件之前,首先了解两个模块:smtplib和email. smtplib模块主要是负责发送邮件的,是一个发邮件的动作,比如连接邮箱服务,登录邮箱,发送邮件等. email模块主要是负责构造邮件 ...

  6. 【实践篇】一次Paas化热部署实践分享

    前言 本文是早些年,Paas化刚刚提出不久时,基于部门内第一次Paas化热部署落地经验所写,主要内容是如何构建一些热部署代码以及一些避雷经验. 一.设计-领域模型设计 1.首先,确定领域服务所属的领域 ...

  7. VMPFC可以融合既有的片段信息来模拟出将来的情感场景

    Ventromedial prefrontal cortex supports affective future simulation by integrating distributed knowl ...

  8. 题解 CF1401C

    题目大意: 给定一序列 \(A\),定义当且仅当 \(\gcd(a_i,a_j)=a_{min}\) 时,元素 \(a_i\) 和 \(a_j\) 可以交换. 问当前给定的序列 \(A\) 能否转化为 ...

  9. Kubernetes 漫游:理解 ConfigMap

    安装说明 通过 docker desktop 可以安装适用于单机和开发环境单机版的 K8S,如果 docker desktop 无法启动 Kubernates 通过以下方式解决: 一:添加国内镜像源 ...

  10. python判断素数

    def slowsnail(num): count = num // 2 while count > 1: if num % count == 0: print('%d最大的约数是%d' % ( ...