首页 > 其他分享 >SpringMVC之域对象共享数据的多种方式

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

时间:2024-01-26 21:56:33浏览次数:20  
标签:return success SpringMVC 向域 测试通过 共享 之域 public

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

使用Servlet向域中共享数据

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

使用ModelAndView向域中共享数据

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

使用Model向域中共享数据

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

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

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

使用ModelMap向域中共享数据

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

使用session向域中共享数据

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

使用ServletContext向域中共享数据

@GetMapping("/testApplication")
public String testApplication(HttpSession session) {
	ServletContext application = session.getServletContext();
	application.setAttribute("testApplication","hello,Application");
	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}">测试通过HttpSession向域中共享数据</a><br/>
<a th:href="@{/testApplication}">测试通过ServletContext向域中共享数据</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="${testServletScope}"></p>
<p th:text="${testModelAndView}"></p>
<p th:text="${testModel}>"></p>
<p th:text="${testMap}"></p>
<p th:text="${testModelMap}"></p>
<p th:text="${testSession}"></p>
<p th:text="${testApplication}"></p>
</body>
</html>

请求页面跳转

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

标签:return,success,SpringMVC,向域,测试通过,共享,之域,public
From: https://www.cnblogs.com/lisong0626/p/17990812

相关文章

  • docker run 共享host网络
    最近在尝试tensorrt-llm,它需要dockerrun一个container,但我在container内部没法从github上下项目。$docker--versionDockerversion24.0.5,build24.0.5-0ubuntu1~22.04.1解决方法:将本机的网络代理修改成global模式连接rundocker的时候输入:dockerrun-it--......
  • 在Rapsberry Pi OS上设置文件夹被多用户共享
    先设置umask为0002全部用户都设置为0002:vi/etc/profile.d/set-umask-for-all-users.sh输入002 如果只是单一用户,vi ~/.bashrc添加:umask002详细介绍看这里:https://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html 一定要设置成002,这一步......
  • Windows远程桌面不能共享剪贴板-Todesk&向日葵&TW等都可以解决
    大家常会遇到mstsc远程桌面的时候,本地和远程之间不能够复制和粘贴文本内容,大家可能会很疑惑,我远程的时候明明在“本地资源”里面勾选了“剪贴板”,但为什么还不能用,原因就是因为“rdpclip.exe”这个进程没有正常工作。解决办法:在服务器上打开任务管理器(Ctrl+Alt+Del),查看......
  • 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.23)
    一、百度网盘SVIP超级会员共享账号可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答。我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免了U盘的物理载体,直接在网上就实现文件传输。百度网盘SVIP会员可以让自己百度账......
  • 无共享存储的在线迁移
    无共享存储的在线迁移 环境需求:最低版本ESXI5.1,保证ESXI主机的cpu型号一样,开启vmotion 一般vSphere集群下的vMotion迁移都需要共享存储,这样,才可以将虚拟机的“主机”与“数据存储”都迁移至另一台上,如下图所示。当使用vSphere5.1的Client,两台ESXi主机也没有共享存储......
  • SpringMVC - 谈谈你对SpringMVC的理解
     谈谈你对SpringMVC的理解?普通人:SpringMVC它是一个MVC框架吧,就是,我们可以使用SpringMVC来开发Web应用...呃它是基于Servlet上的一个扩展,就是它里面我记得好像有一个核心控制器,叫DispatcherServlet,然后扩展了之后,就是所有请求都会经过那个...DispatcherServlet然后再做一......
  • Linux基础45 firewalld防火墙, 参数, 区域配置, 放行策略, 端口转发, 富规则, 防火墙
    firewalld防火墙一、防火墙安全概述在Centos7系统中继承了多款防火墙管理工具,默认启动的是firewalld(动态防火墙管理器)防火墙管理工具,Firewalld支持CLI(命令行)以及(图形)的两种管理方式。对于接触Linux较早的人员对Iptables比较熟悉,但由于Iptables的规则比较的麻烦,并且对网络有......
  • 再回首之SpringMVC深入解析
    第1章:引言大家好,我是小黑,咱们今天来聊聊SpringMVC,SpringMVC其实是Spring的一个模块,专门用来构建Web应用程序。它提供了一种轻量级的方式来构建动态网页。就像小黑我刚开始接触Java时候一样,可能对这些听起来很高大上的东西有点迷茫。不过别担心,咱们一步步来解开SpringMVC的神......
  • 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.21)
    一、百度网盘SVIP超级会员共享账号可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答。我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免了U盘的物理载体,直接在网上就实现文件传输。百度网盘SVIP会员可以让自己百度账......
  • SpringMVC常用注解
    探索SpringMVC常用注解SpringMVC是一个用于构建Web应用程序的框架,它提供了丰富的注解来简化开发过程。在这篇文章中,我们将深入了解一些常用的SpringMVC注解,从它们的起因到实际应用。起因Web应用程序的开发涉及到处理HTTP请求和响应,传统的方式通常需要编写大量的XML配置和复杂的......