Spring MVC域对象共享数据

ops/2024/10/20 18:52:47/

在Spring MVC中,域对象(Domain Object)通常指的是与业务逻辑相关的模型对象,它们代表了应用程序中的核心数据结构。例如,在一个电商应用中,Product、User、Order等类可以被视为域对象。这些对象通常与数据库中的表相对应,并包含业务逻辑和数据验证等功能。

使用ServletAPI向request域对象共享数据

在Spring MVC中,使用HttpServletRequest对象可以直接操作请求范围的属性。通过request.setAttribute()方法设置的属性可以在同一请求的后续处理过程中使用,但它们不会在重定向或后续请求中保持。

java">@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){request.setAttribute("testScope", "hello,servletAPI");return "success";
}

在这个例子中,testScope属性被设置为"hello, servletAPI",并且可以在返回的视图中访问。假设你有一个名为success.jsp的视图,你可以在该视图中使用EL表达式来访问这个属性:

<html>
<body><h1>${testScope}</h1> <!-- 输出: hello, servletAPI -->
</body>
</html>

在这里插入图片描述

使用ModelAndView向request域对象共享数据

在Spring MVC中,使用ModelAndView对象可以方便地将数据传递到视图中。ModelAndView不仅可以设置模型数据,还可以指定视图名称。通过这种方式,你可以将数据放入请求域中,以便在视图中使用。

java">    @RequestMapping("/testModelAndView")public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图,实现页面跳转*/ModelAndView mav = new ModelAndView();//向请求域共享数据mav.addObject("testScope1", "hello,ModelAndView");//设置视图,实现页面跳转mav.setViewName("success");return mav;}
<h5>/testModelAndView</h5>
<p th:text="${testScope1}"></p>
<hr>

在这里插入图片描述

也可以通过这种方式

java">   /*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图,实现页面跳转*/@RequestMapping("/test/mvc")public ModelAndView testMAV(){ModelAndView mav = new ModelAndView();//像请求域中共享数据mav.addObject("name", "miaow");//设计逻辑视图,就是跳转的页面mav.setViewName("success");return mav;}
<h5>/test/mvc</h5>
<a th:href="@{/test/mvc}">获取ModelAndView中的请求域中共享数据 </a>
<p th:text="${name}"></p>
<hr>

在这里插入图片描述

使用Model向request域对象共享数据

在Spring MVC中,使用Model对象可以方便地向请求域共享数据。Model是一个用于传递数据到视图的接口,允许你将属性添加到请求范围中。

java">    @RequestMapping("/testModel")public String testModel(Model model){model.addAttribute("testScope3", "hello,Model");return "success";}
<h5>/testModel</h5>
<p th:text="${testScope3}"></p>
<hr>

在这里插入图片描述

使用map向request域对象共享数据

在Spring MVC中,你可以使用Map对象向请求域共享数据。通过将数据放入Map中,你可以将多个属性传递到视图中。

java">    @RequestMapping("/testMap")public String testMap(Map<String, Object> map){map.put("testScope4", "hello,Map");return "success";}
<h5>/testMap</h5>
<p th:text="${testScope4}"></p>
<hr>

在这里插入图片描述

使用ModelMap向request域对象共享数据

在Spring MVC中,ModelMap是一个用于传递模型数据到视图的对象。它是Model接口的一个实现,允许你将属性添加到请求域中。

java">    @RequestMapping("/testModelMap")public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testScope5", "hello,ModelMap");return "success";}
<h5>/testModelMap</h5>
<p th:text="${testScope5}"></p>
<hr>

在这里插入图片描述

向session域共享数据

在Spring MVC中,向会话(session)域共享数据可以通过使用@SessionAttributes注解或直接使用HttpSession对象来实现。

@SessionAttributes

@SessionAttributes注解可以将特定的模型属性存储在HTTP会话中,这样在用户的多个请求之间可以共享数据。

java">import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;@Controller
@SessionAttributes("user")
public class UserController {@ModelAttribute("user")public User createUserModel() {return new User(); // 创建一个新的User对象}@RequestMapping("/saveUser")public String saveUser(@ModelAttribute("user") User user, Model model) {user.setName("John Doe"); // 设置用户属性model.addAttribute("message", "User saved successfully!");return "success"; // 返回视图名称}@RequestMapping("/showUser")public String showUser(@ModelAttribute("user") User user, Model model) {model.addAttribute("message", "Current User: " + user.getName());return "success"; // 返回视图名称}
}
java">public class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

创建一个名为success.html的Thymeleaf模板文件,用于显示传递的数据。

java"><!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>Session Example</title>
</head>
<body><h1>Response from Controller</h1><p th:text="${message}">This will be replaced by the message value.</p>
</body>
</html>

使用HttpSession

你也可以直接使用HttpSession对象来共享数据。

java">    @RequestMapping("/testSession")public String testSession(HttpSession session){session.setAttribute("testSessionScope", "hello,session");return "success";}
<h5>/testSession</h5>
<p th:text="'Hello,session的值 ' + ${session.testSessionScope} + '!'"></p>
<hr>

在这里插入图片描述

向application域共享数据

在Spring MVC中,向应用程序(application)域共享数据通常是通过ServletContext来实现的。应用程序域的数据在整个应用程序的生命周期内都是可用的,适合于存储全局共享的数据。

java">    @RequestMapping("/testApplication")public String testApplication(HttpSession session){ServletContext application = session.getServletContext();application.setAttribute("testApplicationScope", "hello,application");return "success";}
<h5>/testApplication</h5>
<p th:text="'Hello,application的值 ' + ${application.testApplicationScope} + '!'"></p>

在这里插入图片描述

附上控制层代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>成功跳转页面</title>
</head>
<body><h1>success</h1><hr>
<h1>Response from Controller</h1>
<h5>/testServletAPI</h5>
<p th:text="${testScope}">This will be replaced by the testScope value.</p> <!-- 输出: hello, Thymeleaf! -->
<hr><h5>/testModelAndView</h5>
<p th:text="${testScope1}"></p>
<hr><h5>/test/mvc</h5>
<a th:href="@{/test/mvc}">获取ModelAndView中的请求域中共享数据 </a>
<p th:text="${name}"></p>
<hr><h5>/testModel</h5>
<p th:text="${testScope3}"></p>
<hr><h5>/testMap</h5>
<p th:text="${testScope4}"></p>
<hr><h5>/testModelMap</h5>
<p th:text="${testScope5}"></p>
<hr><h5>/testSession</h5>
<p th:text="'Hello,session的值 ' + ${session.testSessionScope} + '!'"></p>
<hr><h5>/testApplication</h5>
<p th:text="'Hello,application的值 ' + ${application.testApplicationScope} + '!'"></p></body>
</html>
java">@Controller
public class FieldController {@RequestMapping("/testServletAPI")public String testServletAPI(HttpServletRequest request) {request.setAttribute("testScope", "hello, Thymeleaf!");return "success"; // 返回视图名称}@RequestMapping("/testModelAndView")public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图,实现页面跳转*/ModelAndView mav = new ModelAndView();//向请求域共享数据mav.addObject("testScope1", "hello,ModelAndView");//设置视图,实现页面跳转mav.setViewName("success");return mav;}/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图,实现页面跳转*/@RequestMapping("/test/mvc")public ModelAndView testMAV(){ModelAndView mav = new ModelAndView();//像请求域中共享数据mav.addObject("name", "miaow");//设计逻辑视图,就是跳转的页面mav.setViewName("success");return mav;}@RequestMapping("/testModel")public String testModel(Model model){model.addAttribute("testScope3", "hello,Model");return "success";}@RequestMapping("/testMap")public String testMap(Map<String, Object> map){map.put("testScope4", "hello,Map");return "success";}@RequestMapping("/testModelMap")public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testScope5", "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 application = session.getServletContext();application.setAttribute("testApplicationScope", "hello,application");return "success";}
}

总结

  • Request域:适合在单个请求中共享数据,使用Model、ModelMap、Map或ModelAndView。
  • Session域:适合在多个请求之间共享数据,使用@SessionAttributes或HttpSession。
  • Application域:适合在整个应用程序中共享数据,使用ServletContext。

http://www.ppmy.cn/ops/98098.html

相关文章

学习记录——day35 数据库 sqlite3

目录 一、安装sqlite3数据库以及sqlite3函数库 二、数据库的结构 三、常用数据库类型 1、sqlite3 2、mysql 四、sqlite3数据库的使用 1、打开数据库 2、sqlite3数据库中指令的使用 1&#xff09;界面指令 2&#xff09;操作指令 3&#xff09;大小写敏感性 3、创建表…

网络协议(概念版)

通讯&#xff1a;首先要得知对方的IP地址。 最终是根据MAC地址&#xff08;网卡地址&#xff09;&#xff0c;输送数据到网卡&#xff0c;被网卡接收。 如果网卡发现数据的目标MAC地址是自己&#xff0c;就会将数据传递给上一层进行处理;如果目标MAC地址不是自己&#xff0c;…

项目问题 | CentOS 7停止维护导致yum失效的解决办法

目录 centos停止维护意味着yum相关源伴随失效。 报错&#xff1a; 解决方案&#xff1a;将图中四个文件替换掉/etc/yum.repos.d/目录下同名文件 资源提交在博客头部&#xff0c;博客结尾也提供文件源码内容 CentOS-Base.repo CentOS-SCLo-scl.repo CentOS-SCLo-scl-rh.rep…

【JVM】详解Java中的垃圾回收

概念 在传统的编程语言中&#xff0c;内存的管理通常需要由开发人员手动进行分配和释放。这种手动管理容易导致一系列问题&#xff0c;例如内存溢出&#xff08;内存不足时继续申请内存&#xff09;和内存泄漏&#xff08;已不再使用的内存未被释放&#xff09;。这些问题可能…

EXCEL——Vlookup17个高级用法

大纲 一、基本语法 1、参数详解 二、入门篇 1、单条件查找 2、屏蔽查找返回的错误值 三、进阶篇 1、反向查找 2、包含查找 3、区间查找 4、含通配符查找 5、多列查找 6、多区域查找 四、高级篇 1、多条件查找 2、合并单元格查找 3、带合并单元格的多条件查找 …

迁移学习代码复现

一、前言 说来可能令人难以置信,迁移学习技术在实践中是非常简单的,我们仅需要保留训练好的神经网络整体或者部分网络,再在使用迁移学习的情况下把保留的模型重新加载到内存中,就完成了迁移的过程。之后,我们就可以像训练普通神经网络那样训练迁移过来的神经网络了。 我们…

Go语言基础--嵌套循环(多重循环)

嵌套循环可以让你在一个循环内部再嵌套另一个或多个循环。这种结构在处理多维数据&#xff08;如二维数组、矩阵&#xff09;或需要遍历多个集合时非常有用。 for 初始化语句1; 条件判断1; 更新语句1 { // 外层循环体 for 初始化语句2; 条件判断2; 更新语句2 { // 内层循…

Clearpool 推出 Ozean:专注 RWA 的高性能创新区块链

引言 真实资产&#xff08;Real-World Assets, RWA&#xff09;指的是诸如房地产、债券、股票等在现实世界中存在并具有价值的资产。随着 DeFi 的发展&#xff0c;加密创新者们开始探索如何将传统金融市场中的资产引入区块链世界&#xff0c;以扩展 DeFi 的应用范围。然而&…