SpringMVC5-域对象共享数据

目录

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

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

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

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

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

Model、ModelMap、Map的关系

向session域共享数据

向application域共享数据


创建模块springMVC-demo3

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--配置springMVC的编码过滤器--><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--注册前端控制器--><servlet><servlet-name>DispactcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springMVC.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispactcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

springMVC.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"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"><!--扫描组件--><context:component-scan base-package="com.qcby.mvc.controller"></context:component-scan><!-- 配置Thymeleaf视图解析器 --><bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="order" value="1"/><property name="characterEncoding" value="UTF-8"/><property name="templateEngine"><bean class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver"><bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><!-- 视图前缀 --><property name="prefix" value="/WEB-INF/templates/"/><!-- 视图后缀 --><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><property name="characterEncoding" value="UTF-8" /></bean></property></bean></property></bean></beans>

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class TestController {@RequestMapping("/")public String index(){return "index";}
}

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

首页点击超链接,会对应控制器方法,通过ServletAPI向request中共享数据,通过转发跳转到success.html页面,通过thymeleaf获取域对象中的数据值

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class ScopeController {/*** 使用ServletAPI向request域对象共享数据* @return*/@RequestMapping("/testRequestByServletAPI")public String testRequestByServletAPI(HttpServletRequest request){//键值对:键testRequestScope  值hello,ServletAPIrequest.setAttribute("testRequestScope","hello,ServletAPI");return "success";}
}

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testRequestByServletAPI}">通过ServletAPI向request域对象共享数据</a>
</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>
success<br>
<p th:text="${testRequestScope}"></p>
</body>
</html>

 

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

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;@Controller
public class ScopeController {@RequestMapping("/testModelAndView")public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图,实现页面跳转*/ModelAndView mav = new ModelAndView();//处理模型数据,即向请求域request共享数据mav.addObject("testRequestScope","hello,ModelAndView");//设置视图名称mav.setViewName("success");return mav;}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a>
</body>
</html>

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

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;@Controller
public class ScopeController {@RequestMapping("/testModel")public String testModel(Model model){model.addAttribute("testRequestScope","hello,Model");return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a>
</body>
</html>

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

package com.qcby.mvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import java.util.Map;@Controller
public class ScopeController {@RequestMapping("/testMap")public String testMap(Map<String,Object> map){map.put("testRequestScope","hello,map");return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a>
</body>
</html>

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

package com.qcby.mvc.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.http.HttpServletRequest;
import java.util.Map;@Controller
public class ScopeController {@RequestMapping("/testModelMap")public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testRequestScope","hello,ModelMap");return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a>
</body>
</html>

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域共享数据

package com.qcby.mvc.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.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;@Controller
public class ScopeController {@RequestMapping("/testSession")public String testSession(HttpSession session){session.setAttribute("testSessionScope","hello,session");return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testSession}">通过ServletAPI向session域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
success<br>
<p th:text="${session.testSessionScope}"></p>
</body>
</html>

 

向application域共享数据

package com.qcby.mvc.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;@Controller
public class ScopeController {@RequestMapping("/testApplicationScope")public String testApplicationScope(HttpSession session){ServletContext application = session.getServletContext();application.setAttribute("testApplicationScope","hello,application");return "success";}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testApplicationScope}">通过ServletAPI向application域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
success<br>
<p th:text="${application.testApplicationScope}"></p>
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/54873.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

asp.net core grpc快速入门

环境 .net 8 vs2022 创建 gRPC 服务器 一定要勾选Https 安装Nuget包 <PackageReference Include"Google.Protobuf" Version"3.28.2" /> <PackageReference Include"Grpc.AspNetCore" Version"2.66.0" /> <PackageR…

Python | Leetcode Python题解之第441题排列硬币

题目&#xff1a; 题解&#xff1a; class Solution:def arrangeCoins(self, n: int) -> int:left, right 1, nwhile left < right:mid (left right 1) // 2if mid * (mid 1) < 2 * n:left midelse:right mid - 1return left

Junit 5 - 理解Mockito,提高UT 覆盖率

前言 当我是1个3年初级程序员时&#xff0c; 我被面试者问到1个问题&#xff1a; 如何保证你的开发任务交付质量 当我是1个7年开发组长时&#xff0c; 我被面试者问到另1个问题&#xff1a;如何保证你的团队的代码质量&#xff0c; 减少rework。 又若干年后&#xff0c; 我才…

Mysql调优之索引优化(四)

一、mysql索引结构B树原理 B树开始就是n树&#xff0c;不是二叉树 B树的非叶子结点存储了数据&#xff0c;导致层级会很深&#xff0c;每一层又有数据又有索引。 B树只有叶子结点存储数据&#xff0c;其余都是存储索引&#xff0c;增加了每层存取索引的数量&#xff08;3层结构…

Comfyui 学习笔记1

如果图像输出被裁剪&#xff0c;则需要使用PrepImageForClipVision&#xff0c;来设置图像距离上边沿的位置. 决定绘画的作用区域&#xff0c;后面的KSample只作用到 mask标记的范围。 图像位置偏移了&#xff0c;可以考虑通过Image crop 裁剪 IPAdapter face 提取时&…

OceanBase 3.X 高可用 (一)

OceanBase 3.X 高可用&#xff08;一&#xff09; 一、分布式核心 OceanBase 3.x 采用的是paxos 协议&#xff0c;与raft协议相比。其复杂程度高&#xff0c;实现技术难度大。 Paxos 协议允许事务日志乱序发送&#xff0c;顺序提交。raft允许事务顺序发送&#xff0c;顺序提…

的使用和内联函数

今天我们来了解一下C中的&和内联函数 引用标识符& C觉得C语言部分的指针有些麻烦&#xff0c;容易混乱&#xff0c;所以C创造了一个标识符&&#xff0c;表示是谁的别名。跟指针对比一下&#xff1a;int* a1&b1;int &a2b2;这样看&#xff0c;显然a1存放的…

【java】前端RSA加密后端解密

目录 1. 说明2. 前端示例3. 后端示例3.1 pom依赖3.2 后端结构图3.3 DecryptHttpInputMessage3.4 ApiCryptoProperties3.5 TestController3.6 ApiCryptoUtil3.7 ApiDecryptParamResolver3.8 ApiDecryptRequestBodyAdvice3.9 ApiDecryptRsa3.10 ApiCryptoProperties3.11 KeyPair3…

爱速搭百度低代码开发平台

爱速搭介绍 爱速搭是百度智能云推出的低代码开发平台&#xff0c;它灵活性强&#xff0c;对开发者友好&#xff0c;在百度内部大规模使用&#xff0c;有超过 4w 内部页面是基于它制作的&#xff0c;是百度内部中台系统的核心基础设施。 它具备以下功能&#xff1a; 页面制作…

Android界面控件概述

节选自《Android应用开发项目式教程》&#xff0c;机械工业出版社&#xff0c;2024年7月出版 做最简单的安卓入门教程&#xff0c;手把手视频、代码、答疑全配齐 控件是Android界面的重要组成单元&#xff0c;Android应用主要通过控件与用户交互&#xff0c;Android提供了非常…

raise Exception(“IPAdapter model not found.“)

IPAdapter模型文件太多了&#xff0c;而节点IPAdapter Unified Loader是通过函数&#xff08;get_ipadapter_file与get_clipvision_file&#xff09;预设来加载模型文件&#xff0c;当发生错误“IPAdapter model not found.“时并不指明模型文件名&#xff0c;导致想要有针对性…

在MacOS上安装MongoDB数据库

一、安装方法 1.1 安装包安装 首先&#xff0c;打开MongoDB 官网下载安装包&#xff0c;下载链接&#xff1a;https://www.mongodb.com/try/download/community。 根据自己的系统环境自行选择下载的版本。将下载好的 MongoDB 安装包解压缩&#xff0c;并将文件夹名改为 mon…

Go语言接口与多态

Go语言虽然并非传统意义上的面向对象语言&#xff0c;但它通过接口&#xff08;Interface&#xff09;和匿名组合&#xff08;Composition&#xff09;等机制&#xff0c;实现了类似面向对象编程中的多态性&#xff08;Polymorphism&#xff09;。接口和多态性是Go语言中非常重…

信息技术对现代商业的推动作用及未来趋势

信息技术对现代商业的推动作用及未来趋势 目录 引言信息技术对商业的推动作用 业务流程自动化供应链优化客户关系管理电子商务的崛起 信息技术对不同行业的影响 零售行业制造业金融行业 信息技术带来的挑战 数据隐私问题技术迭代的压力信息孤岛现象 未来信息技术的发展趋势 人…

开源大模型 vs闭源大模型

在人工智能&#xff08;AI&#xff09;领域&#xff0c;如何评价一个AI模型的优劣和发展前景&#xff0c;是一个复杂而又广泛讨论的问题。在这个过程中&#xff0c;"开源"和"闭源"的发展路径成为绕不开的两条道路。开源模式以共享知识和技术进步为宗旨&…

理解:基础地理实体相关概述

理解&#xff1a;基础地理实体相关概述 地理实体 geo-entity 现实世界中占据一定且连续空间位置和范围、单独具有同一属 性或完整功能的地理对象。 基础地理实体 fundamental geo-entity 通过基础测绘采集和表达的地理实体&#xff0c;是其他地理实体和相关 信息的定位框架与…

卡通角色检测系统源码分享

卡通角色检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer Vis…

Keepalived+MySQL 高可用集群

基础架构如下 准备干净的实验环境 [rootmysql1 ~]# systemctl stop firewalld [rootmysql1 ~]# cat /etc/sysconfig/selinux |grep "SELINUXdisabled" SELINUXdisabled [rootmysql1 ~]# setenforce 0 setenforce: SELinux is disabled [rootmysql1 ~…

IoT网关的主要功能有哪些?天拓四方

在数字化浪潮席卷全球的今天&#xff0c;物联网&#xff08;IoT&#xff09;技术凭借其独特的优势&#xff0c;逐渐在各个领域展现出强大的生命力。而IoT网关&#xff0c;作为连接物理世界与数字世界的桥梁&#xff0c;其在物联网体系中的作用愈发凸显。 一、数据聚合与预处理…

acw(树的重心)

给定一颗树&#xff0c;树中包含 n&#x1d45b; 个结点&#xff08;编号 1∼n1∼&#x1d45b;&#xff09;和 n−1&#x1d45b;−1 条无向边。 请你找到树的重心&#xff0c;并输出将重心删除后&#xff0c;剩余各个连通块中点数的最大值。 重心定义&#xff1a;重心是指树…