SpringMVC之域对象共享数据的多种方式

在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在同一 Web 应用程序的多个组件中进行共享和访问。常见的共享域有四种:ServletContextHttpSessionHttpServletRequestPageContext

  1. ServletContext 共享域:ServletContext 对象可以在整个 Web 应用程序中共享数据,是最大的共享域。一般可以用于保存整个 Web 应用程序的全局配置信息,以及所有用户都共享的数据。在 ServletContext 中保存的数据是线程安全的。
  2. HttpSession 共享域:HttpSession 对象可以在同一用户发出的多个请求之间共享数据,但只能在同一个会话中使用。比如,可以将用户登录状态保存在 HttpSession 中,让用户在多个页面间保持登录状态。
  3. HttpServletRequest 共享域:HttpServletRequest 对象可以在同一个请求的多个处理器方法之间共享数据。比如,可以将请求的参数和属性存储在 HttpServletRequest 中,让处理器方法之间可以访问这些数据。
  4. PageContext 共享域:PageContext 对象是在 JSP 页面Servlet 创建时自动创建的。它可以在 JSP 的各个作用域中共享数据,包括pageScoperequestScopesessionScopeapplicationScope 等作用域。

共享域的作用是提供了方便实用的方式在同一 Web 应用程序的多个组件之间传递数据,并且可以将数据保存在不同的共享域中,根据需要进行选择和使用。

本次场景演示使用Thymeleaf服务器渲染技术。

向Request域中共享数据

使用HttpServletRequest向域中共享数据

@GetMapping("/testServletScope")
public String testServlet(HttpServletRequest request) {
     request.setAttribute("testRequestScope", "hello,servlet");
     return "success";
}

使用ModelAndView向域中共享数据

@GetMapping("/testModelAndView")
public ModelAndView testModelAndView() {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject("testRequestScope", "hello,ModelAndView");
	modelAndView.setViewName("success");
	return modelAndView;
}

使用Model向域中共享数据

@GetMapping("/testModel")
public String testModel(Model model) {
	model.addAttribute("testRequestScope", "hello,Model");
	return "success";
}

使用Map集合向域中共享数据

@GetMapping("/testMap")
public String testMap(Map<String, Object> map) {
	map.put("testRequestScope","hello,Map");
	return "success";
}

使用ModelMap向域中共享数据

@GetMapping("/testModelMap")
public String testModelMap(ModelMap modelMap) {
	modelMap.addAttribute("testRequestScope","hello,ModelMap");
	return "success";
}

向session域中共享数据

@GetMapping("/testSession")
public String testSession(HttpSession session) {
	session.setAttribute("testSessionScope","hello,session");
	return "success";
}

向Application域中共享数据

//方式1
@GetMapping("/testApplication")
public String testApplication(HttpSession session) {
	ServletContext application = session.getServletContext();
	application.setAttribute("testApplicationScope","hello,Application");
	return "success";
}

//方式2
//springmvc会在初始化容器的时候,将ServletContext对象存储到IoC容器中
@Autowired
private ServletContext servletContext;

@GetMapping("/testApplication2")
public String testApplication2(HttpSession session) {
	ServletContext application = session.getServletContext();
	application.setAttribute("testApplicationScope","Hello,ApplicationContext");
	return "success";
}

测试

创建testScope.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试域对象</title>
</head>
    <body>
        <a th:href="@{/testServletScope}">测试通过Servlet向域中共享数据</a><br/>
        <a th:href="@{/testModelAndView}">测试通过ModelAndView向域中共享数据</a><br/>
        <a th:href="@{/testModel}">测试通过Model向域中共享数据</a><br/>
        <a th:href="@{/testMap}">测试通过Map集合向域中共享数据</a><br/>
        <a th:href="@{/testModelMap}">测试通过ModelMap向域中共享数据</a><br/>
        <a th:href="@{/testSession}">测试向Session域中共享数据</a><br/>
        <a th:href="@{/testApplication}">测试向Application域中共享数据</a><br/>
    </body>
</html>

创建success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>SUCCESS...</h2>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSessionScope}"></p>
<p th:text="${application.testApplication}"></p>
</body>
</html>

请求页面跳转

@GetMapping("/testScope")
public String testScope() {
    return "testScope";
}

相关文章

开发过程中是不可避免地会出现各种异常情况的,例如网络连接...
说明:使用注解方式实现AOP切面。 什么是AOP? 面向切面编程...
Spring MVC中的拦截器是一种可以在请求处理过程中对请求进行...
在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在...
文件上传 说明: 使用maven构建web工程。 使用Thymeleaf技术...
创建初始化类,替换web.xml 在Servlet3.0环境中,Web容器(To...