Spring MVC 获取三个域(request请求域,session 会话域,application 应用 ...

飞不高  金牌会员 | 2024-6-30 17:43:25 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 870|帖子 870|积分 2610

1. Spring MVC 获取三个域(request请求域,session 会话域,application 应用域)对象的方式

@
目录

2. Servlet中的三个域对象

Servlet 中的三个域对象分别是:
请求域:request
会话域:session
应用域:application
三个域都有以下三个方法:
  1. // 向域中存储数据
  2. void setAttribute(String name, Object obj);
  3. // 从域中读取数据
  4. Object getAttribute(String name);
  5. // 删除域中的数据
  6. void removeAttribute(String name);
复制代码
重要是通过:setAttribute +  getAttribute 方法来完成在域中数据的传递和共享。
request:
接口名:HttpServletRequest
简称:request
request对象代表了一次请求。一次请求一个request。
使用请求域的业务场景:
在A资源中通过转发的方式跳转到B资源,由于是转发,因此从A到B是一次请求,假如想让A资源和B资源共享同一个数据,可以将数据存储到request域中。
session:
接口名:HttpSession
简称:session
session对象代表了一次会话。从打开欣赏器开始访问,到最终欣赏器关闭,这是一次完整的会话。每个会话session对象都对应一个JSESSIONID,而JSESSIONID生成后以cookie的方式存储在欣赏器客户端。欣赏器关闭,JSESSIONID失效,会话竣事。
使用会话域的业务场景:

  • 在 A 资源中通过重定向(重定向是一次新的请求)的方式转到 B 资源,由于是重定向,因此从 A到 B 是两次请求,假如想让 A 资源和 B 资源共享同一个数据,可以将数据存储到 session域中
  • 登录成功后保存用户的登录状态
application
接口名:ServletContext
简称:application
application对象代表了整个  web 应用,服务器启动的创建,服务器关闭时烧毁,对于一个 web 应用来说,application 对象只有一个。
使用应用域的业务场景:记载网站的在线人数。
3. 准备工作

3.1 创建模块,添加依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.rainbowsea</groupId>
  7.     <artifactId>springmvc-004-blog</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <packaging>war</packaging>
  10.     <properties>
  11.         <maven.compiler.source>17</maven.compiler.source>
  12.         <maven.compiler.target>17</maven.compiler.target>
  13.     </properties>
  14.     <dependencies>
  15.         
  16.         <dependency>
  17.             <groupId>org.springframework</groupId>
  18.             <artifactId>spring-webmvc</artifactId>
  19.             <version>6.1.4</version>
  20.         </dependency>
  21.         
  22.         <dependency>
  23.             <groupId>ch.qos.logback</groupId>
  24.             <artifactId>logback-classic</artifactId>
  25.             <version>1.5.3</version>
  26.         </dependency>
  27.         
  28.         <dependency>
  29.             <groupId>jakarta.servlet</groupId>
  30.             <artifactId>jakarta.servlet-api</artifactId>
  31.             <version>6.0.0</version>
  32.             <scope>provided</scope>
  33.         </dependency>
  34.         
  35.         <dependency>
  36.             <groupId>org.thymeleaf</groupId>
  37.             <artifactId>thymeleaf-spring6</artifactId>
  38.             <version>3.1.2.RELEASE</version>
  39.         </dependency>
  40.     </dependencies>
  41. </project>
复制代码
3.2 添加 web 支持

先在 main 目录下,添加名为 webapp 的目录(文件夹),只能是这个 webapp 目录名,不可以是其他的。



3.3 编写 web.xml 文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
  5.          version="5.0">
  6.    
  7.     <servlet>
  8.         <servlet-name>springmvc</servlet-name>
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10.         
  11.         <init-param>
  12.             <param-name>contextConfigLocation</param-name>
  13.             <param-value>classpath:springmvc.xml</param-value>
  14.         </init-param>
  15.         
  16.         <load-on-startup>1</load-on-startup>
  17.     </servlet>
  18.     <servlet-mapping>
  19.         <servlet-name>springmvc</servlet-name>
  20.         <url-pattern>/</url-pattern>
  21.     </servlet-mapping>
  22. </web-app>
复制代码
3.4 创建 IndexController 类

创建 IndexController 类作为 首页来使用
  1. package com.rainbowsea.springmvc.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller // 交给 Spring IOC 容器管理
  5. public class IndexController {
  6.     @RequestMapping("/")
  7.     public String index() {
  8.         return "index";
  9.     }
  10. }
复制代码
3.5 编写 springmvc.xml

在 springmvc.xml 当中配置两个信息:

  • 组件扫描
  • 视图分析器
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6.    
  7.     <context:component-scan base-package="com.rainbowsea.springmvc.controller"></context:component-scan>
  8.    
  9.     <bean id="thymeleafViewResolver" >
  10.         
  11.         <property name="characterEncoding" value="UTF-8"/>
  12.         
  13.         <property name="order" value="1"/>
  14.         
  15.         <property name="templateEngine">
  16.             <bean >
  17.                
  18.                 <property name="templateResolver">
  19.                     <bean >
  20.                         
  21.                         <property name="prefix" value="/WEB-INF/templates/"/>
  22.                         
  23.                         <property name="suffix" value=".html"/>
  24.                         
  25.                         <property name="templateMode" value="HTML"/>
  26.                         
  27.                         <property name="characterEncoding" value="UTF-8"/>
  28.                     </bean>
  29.                 </property>
  30.             </bean>
  31.         </property>
  32.     </bean>
  33. </beans>
复制代码
3.6 编写 index.html 文件视图

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>
复制代码
3.7 部署测试


4. Spring MVC 获取 request 请求域对象的五种方式

在SpringMVC中,在request域中共享数据有以下五种方式:

  • 使用原生Servlet API方式。
  • 使用Model接口。
  • 使用Map接口。
  • 使用ModelMap类。
  • 使用ModelAndView类。
4.1 第一种方式:使用原生Servlet API方式 获取到 request 请求域,同时获取到请求域当中对应的内容

第一种方式: 在Spring MVC 中使用原生的 Servlet API 可以完成 request 域数据共享
,在处置惩罚器方法上添加 HttpServletRequest 参数即可。
将 HttpServletRequest 作为参数,定义到方法上,Spring MVC 框架会自动从 Tomcat 服务器当中获取到这个 HttpServletRequest 对象的值,然后,传递给这个方法的 HttpServletRequest 参数值上,完成赋值。
创建一个 RequestScopeTestController 类,注意要交给 Spring IOC 容器管理起来
  1. package com.rainbowsea.springmvc.controller;
  2. import jakarta.servlet.http.HttpServletRequest;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.ui.ModelMap;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. import java.util.Map;
  9. @Controller // 交给Spring IOC 容器管理
  10. public class RequestScopeTestController {
  11.     // request 请求域
  12.     @RequestMapping("/testServletAPI")
  13.     public String testServletAPI(HttpServletRequest request) {
  14.         // 将共享的数据存储到 request域当中
  15.         // 跳转视图,在视图页面将request域中的数据取出,这样就完成了,Controller和View在同一个请求当中两个组件之间的数据共享
  16.         // 将共享的数据存储到request域当中
  17.         request.setAttribute("testRequestScope", "在SpringMVC当中使用原生Servlet API 完成 request域的数据共享");
  18.         System.out.println(request);
  19.         System.out.println(request.getClass().getName());
  20.         // 跳转视图,在视图页面将 request 域中的数据取出来,这样就完成了,Controller 和 View 在同
  21.         // 一个请求当中两个组件之间数据的共享
  22.         // 注意:这个是跳转,默认情况下是,转发的方式,(转发 forward 是一次请求)
  23.         // 这个返回的是一个逻辑视图名称,经过视图解析器解析,变成物理视图名称,/WEB-INF/templates/ok.html
  24.         return "ok";
  25.     }
  26. }
复制代码
index页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
复制代码
ok 页面获取 request 请求域的展示
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
复制代码
测试结果:

这种方式当然可以,用 SpringMVC 框架,不建议使用原生 Servlet API
4.2 第二种方式:使用 Model 接口 获取到 request 请求域,同时获取到请求域当中对应的内容

  1. import jakarta.servlet.http.HttpServletRequest;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import java.util.Map;
  8. @Controller // 交给Spring IOC 容器管理
  9. public class RequestScopeTestController {
  10.     @RequestMapping(value = "/testModel")
  11.     public String testModel(Model model) {
  12.         // 向 request 域当中绑定数据
  13.         model.addAttribute("testRequestScope", "在SpringMVC 当中使用 Model 接口完成 request 域数据共享");
  14.         System.out.println(model);
  15.         System.out.println(model.getClass().getName());
  16.         // 转发
  17.         return "ok";
  18.     }
  19. }
复制代码
index 页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
  13. [url=https://www.cnblogs.com/@{/testModel}]测试在 Spring MVC 当中使用 Model 接口完成 request 域数据共享[/url]
复制代码
ok 页面获取 request 请求域的展示
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http//www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>OK</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
复制代码
启动 Tomcat 服务器测试结果:

4.3 第三种方式:使用Map接口 获取到 request 请求域,同时获取到请求域当中对应的内容

  1. import jakarta.servlet.http.HttpServletRequest;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import java.util.Map;
  8. @Controller // 交给Spring IOC 容器管理
  9. public class RequestScopeTestController {
  10.     @RequestMapping(value = "/testMap")
  11.     public String testMap(Map<String, Object> map) {
  12.         // 向 request 域当中的存储数据
  13.         map.put("testRequestScope", "在Spring MVC 当中使用 Map接口完成 request 域数据共享");
  14.         System.out.println(map);
  15.         System.out.println(map.getClass().getName());
  16.         // 转发
  17.         return "ok";
  18.     }
  19. }
复制代码
index 页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
  13. [url=https://www.cnblogs.com/@{/testModel}]测试在 Spring MVC 当中使用 Model 接口完成 request 域数据共享[/url]
  14. [url=https://www.cnblogs.com/@{/testMap}]测试在Spring MVC 当中使用 Map 接口完成 request 域数据共享[/url]
复制代码
ok 页面获取 request 请求域的展示 保持不变
启动 Tomcat 服务器测试结果:

4.4 第四种方式:使用 ModelMap 类 获取到 request 请求域,同时获取到请求域当中对应的内容

  1. import jakarta.servlet.http.HttpServletRequest;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import java.util.Map;
  8. @Controller // 交给Spring IOC 容器管理
  9. public class RequestScopeTestController {
  10.     @RequestMapping(value = "/testModelMap")
  11.     public String testModelMap(ModelMap modelMap) {
  12.         // 向 request 域当中存储数据
  13.         modelMap.addAttribute("testRequestScope", "在Spring MVC 当中 ModelMap类完成request 域数据共享");
  14.         System.out.println(modelMap);
  15.         System.out.println(modelMap.getClass().getName());
  16.         return "ok";
  17.     }
  18. }
复制代码
index 页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
  13. [url=https://www.cnblogs.com/@{/testModel}]测试在 Spring MVC 当中使用 Model 接口完成 request 域数据共享[/url]
  14. [url=https://www.cnblogs.com/@{/testMap}]测试在Spring MVC 当中使用 Map 接口完成 request 域数据共享[/url]
  15. [url=https://www.cnblogs.com/@{/testModelMap}]测试在Spring MVC当中使用 ModelMap 类完成 request 域数据共享[/url]
复制代码
ok 页面获取 request 请求域的展示 保持不变
启动 Tomcat 服务器测试结果:

4.5 增补:Model、Map、ModelMap的关系

可以在以上Model、Map、ModelMap的测试程序中将其输出,看看输出什么:

看不出来什么区别,从输出结果上可以看到都是一样的。
可以将其运行时类名输出:


通过输出结果可以看出,无论是Model、Map还是ModelMap,底层实例化的对象都是:BindingAwareModelMap。
可以检察 BindingAwareModelMap的继承结构:

通过继承结构可以看出:BindingAwareModelMap继承了ModelMap,而ModelMap又实现了Map接口。
另外,请看以下源码:

可以看出ModelMap又实现了Model接口。因此表面上是接纳了差别方式,底层本质上是相同的。
SpringMVC之以是提供了这些方式,目标就是方便程序员的使用,提供了多样化的方式,可见它的重要性。
4.6 第五种方式:使用 ModelAndView 类获取到 request 请求域,同时获取到请求域当中对应的内容

在 Spring MVC 框架中为了更好的体现 MVC 架构模式,提供了一个类:ModelAndView。
这个类的实例封装了 Model 和 View 。也就是说 这个类封装业务处置惩罚之后的数据,也体现了跳转到哪个视图。使用它也可以完成  request 域数据共享。
使用这种方式的注意事项:

  • 方法的返回值范例不是 String 了,而是 ModelAndView 对象
  • ModelAndView 不是出现在方法的参数位置上了,而是在方法体中 new 出来的
  • 需要调用 addObject() 向域中存储数据
  • 需要调用 setViewName() 设置视图的名字
  • 末了需要将 ModelAndView 作为参数,返回。
  1. import jakarta.servlet.http.HttpServletRequest;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import java.util.Map;
  8. @Controller // 交给Spring IOC 容器管理
  9. public class RequestScopeTestController {
  10.     @RequestMapping(value = "/testModelAndView")
  11.     public ModelAndView testModelAndView() {
  12.         // 创建模型视图对象
  13.         ModelAndView modelAndView = new ModelAndView();
  14.         // 给模型视图对象绑定数据
  15.         modelAndView.addObject("testRequestScope", "在SpringMVC当中使用 ModelAndView 类完成 request 域数据共享");
  16.         // 给模型视图对象 绑定视图(绑定逻辑视图名称)
  17.         modelAndView.setViewName("ok");
  18.         // 返回模型视图对象
  19.         return modelAndView;
  20.     }
  21. }
复制代码
index页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试 request 域对象</h2>
  11. </body>
  12. </html>[url=https://www.cnblogs.com/@{/testServletAPI}]测试在SpringMVC当中使用原生 Servlet API 完成 request 域的数据共享[/url]
  13. [url=https://www.cnblogs.com/@{/testModel}]测试在 Spring MVC 当中使用 Model 接口完成 request 域数据共享[/url]
  14. [url=https://www.cnblogs.com/@{/testMap}]测试在Spring MVC 当中使用 Map 接口完成 request 域数据共享[/url]
  15. [url=https://www.cnblogs.com/@{/testModelMap}]测试在Spring MVC当中使用 ModelMap 类完成 request 域数据共享[/url]
  16. [url=https://www.cnblogs.com/@{/testModelAndView}]测试在Spring MVC当中使用 ModelAndView 类完成 request 域数据共享[/url]
复制代码
ok 页面获取 request 请求域的展示的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http//www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>OK</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
复制代码
启动 Tomcat 服务器测试结果:

4.7 增补:ModelAndView 源码分析

以上我们通过了五种方式完成了request域数据共享,包括:原生 Servlet API,Model、Map、ModelMap、ModelAndView
其中后四种:Model、Map、ModelMap、ModelAndView。
这四种方式在底层DispatcherServlet调用我们的Controller之后,返回的对象都是ModelAndView,
这个可以通过源码进行分析。
在以上四种方式中,拿Model举例,添加断点进行调试:

启动服务器,发送请求,走到断点:

检察VM Stack信息:

检察 DispatcherServlet 的1089行,源码如下:

可以看到这里,无论你使用哪种方式,最终都要返回一个ModelAndView对象。
对于 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); 处置惩罚器方法来说,不管你使用的参数是 Model接口,还是Map接口,还是ModelMap类,还是ModelAndView类,最终处置惩罚器方法实行竣事之前
返回的都是 ModelAndView对象,这个返回的ModelAndView对象给DispatcherServlet类了
当请求路径不是JSP的时候,都会走前端控制器 DispatcherServlet
DispatcherServlet 中有一个焦点方法 doDispatch(),这个方法用来通过请求路径找到对应的处置惩罚器方法
然后调用处置惩罚器方法,处置惩罚器方法返回一个逻辑视图名称(大概也会直接返回一个ModelAndView对象)
,返回给DispatcherServlet
提醒:各人可以通过以下断点调试方式,接纳一级一级返回,最终可以看到都会返回ModelAndView对象。

5. Spring MVC 获取 session 会话域对象的二种方式

在SpringMVC中使用session域共享数据,实现方式有多种,其中比较常见的两种方式:

  • 使用原生Servlet API
  • 使用SessionAttributes注解
5.1 第一种方式:使用原生Servlet API 获取到 session 会话域,同时获取到 session 会话域当中的信息

使用原生Servlet API ,就是将 HttpSession 作为方法的参数形式,Spring MVC 会自动获取到 Tomcat 服务器当中的 HttpSession 对象,赋值到这个方法的对应的 HttpSession 参数上。
创建:SessionScopeTestController 类
  1. import jakarta.servlet.http.HttpSession;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.ModelMap;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.SessionAttributes;
  6. @Controller // 交给 Spring IOC 容器管理
  7. public class SessionScopeTestController {
  8.     @RequestMapping("/testSessionServletAPI")
  9.     public String testServletAPI(HttpSession session) {
  10.         // 处理核心业务...
  11.         //将数据存储到 Session中
  12.         session.setAttribute("testSessionScope","在Spring MVC 当中使用原生 Servlet API 完成 session 域数据共享");
  13.         // 返回逻辑视图(这是一个转发的行为)
  14.         return "ok";
  15.     }
  16. }
复制代码
index 页面超链接的编写:

ok 页面获取 request 请求域的展示的编写
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http//www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>OK</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
复制代码
启动 Tomcat 服务器测试结果:

5.2 第二种方式:使用 @SessionAttributes 注解 获取到 session 会话域,同时获取到 session 会话域当中的信息

使用 @SessionAttributes 注解标注 Controller:
  1. @SessionAttributes(value = {"x","y","testSessionScope"}) // 标注 x 和 y 都是存放到 session 域中,而不是 request域
复制代码
**注意:@SessionAttributes  注解使用在Controller类上。标注了当 key是 x 或者 y 时,该(x 或 y)的数据将被存储到会话 session域当中中。而假如没有 SessionAttributes注解,默认存储到 request域中。 **
  1. import jakarta.servlet.http.HttpSession;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.ModelMap;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.SessionAttributes;
  6. @SessionAttributes(value = {"x","y","testSessionScope"}) // 标注 x 和 y 都是存放到 session 域中,而不是 request域
  7. @Controller // 交给 Spring IOC 容器管理
  8. public class SessionScopeTestController {
  9.     @RequestMapping(value = "/testSessionAttributes")
  10.     public String testSessionAttributes(ModelMap modelMap) {
  11.         // 处理业务
  12.         // 将数据存储到 Session域当中
  13.         modelMap.addAttribute("testSessionScope","在Spring MVC 当中使用@SessionAttributes 注解完成 session 域数据共享");
  14.         modelMap.addAttribute("x","李华");
  15.         modelMap.addAttribute("y","小红");
  16.         return "ok";
  17.     }
  18. }
复制代码
index 页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试Session域对象</h2>
  11. <a th:target="_blank" href="https://www.cnblogs.com/@{/testSessionAttributes}">测试在Spring MVC 当中使用 @SessionAttributes 注解完成 session域数据共享</a>

  12. </body>
  13. </html>
复制代码
ok 页面获取 request 请求域的展示的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http//www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>OK</title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
复制代码
启动 Tomcat 服务器测试结果:

6. Spring MVC 获取 application 应用域对象的方式

在SpringMVC实现application域数据共享,最常见的方案就是直接使用Servlet API了:
这个 application 域使用较少,假如使用的话,一样平常是接纳 ServletAPI的方式使用
创建:ApplicationScopeTestController 类
将 HttpServletRequest 作为参数,定义到方法上,Spring MVC 框架会自动从 Tomcat 服务器当中获取到这个 HttpServletRequest 对象的值,然后,传递给这个方法的 HttpServletRequest 参数值上,完成赋值。
  1. package com.rainbowsea.springmvc.controller;
  2. import jakarta.servlet.ServletContext;
  3. import jakarta.servlet.http.HttpServletRequest;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. @Controller // 交给 Spring IOC 容器管理
  7. public class ApplicationScopeTestController {
  8.     @RequestMapping("/testApplicationScope")
  9.     public String testApplicationScope(HttpServletRequest request) {
  10.         // 将数据存储到application域当中
  11.         // 获取application对象,其实就是获取 ServletContext对象
  12.         // 怎么获取 ServletContext对象/通过 request,通过 session都可以用
  13.         ServletContext application = request.getServletContext();
  14.         application.setAttribute("testApplicationScope", "在Spring MVC 中使用 Servlet API中实现application域共享");
  15.         return "ok";
  16.     }
  17. }
复制代码
index 页面超链接的编写:
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>测试三个对象</title>
  6. </head>
  7. <body>
  8. <h1>测试三个域对象</h1>
  9. <hr>
  10. <h2>测试Application应用域对象</h2>
  11. <a th:target="_blank" href="https://www.cnblogs.com/@{/testApplicationScope}">测试在Spring MVC 当中使用 Servlet API 实现application域数据共享</a>

  12. </body>
  13. </html>
复制代码
ok 页面获取 request 请求域的展示的编写

启动 Tomcat 服务器测试结果:

7. 总结:


  • 三个域:request 请求域,session 会话域,application 应用域
  • 三者域的使用,从最小范围的域,来判断使用,可以用范围更小的域,就用范围更小的域来解决题目,传数据资源。假如域的范围不够,就一点的扩大。
  • 在SpringMVC中,在request域中共享数据有以下五种方式:

    • 使用原生Servlet API方式。
    • 使用Model接口。
    • 使用Map接口。
    • 使用ModelMap类。
    • 使用ModelAndView类。

  • 在SpringMVC中使用session域共享数据,实现方式有多种,其中比较常见的两种方式:

    • 使用原生Servlet API
    • 使用SessionAttributes注解

  • Spring MVC 获取 application 应用域对象的方式
8. 末了:

“在这个末了的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上汲取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表