SpringMvc—域对象共享数据和视图

一、向request域创建对象

先创建首页:

在testController这个类中:

package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class testController {@RequestMapping("/")public String index(){return "index";}
}

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>

1.使用servletAPI向request域对象共享数据

在scopeController这个类中:

package com.pon.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class scopeController {
@RequestMapping("/scope")public String Scope(HttpServletRequest request){request.setAttribute("GetscopeObject","Hello Servlret");return "success";}
}

在success.html中:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

在首页中要跳转到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="@{/scope}">Servlet API域对象共享</a>
</body>
</html>

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

在scopeController中:

@Controller
public class scopeController {
@RequestMapping("/mv")public ModelAndView testmv(){ModelAndView mav=new ModelAndView();//向请求域request共享数据mav.addObject("GetscopeObject","Hello ModelandView");//设置视图名称mav.setViewName("success");return mav;}
}

在success.html中:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

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

在scopeController中:

@Controller
public class scopeController {@RequestMapping("/m")public String testModel(Model model){model.addAttribute("GetscopeObject","hello model");return "success";}
}

success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${GetscopeObject}"></p>
</body>
</html>

 

4.使用Map向request域对象共享数据

在scopeController中:

@Controller
public class scopeController {
@RequestMapping("/map")public String testMap(Map<String, Object> map){map.put("GetscopeObject","hello map");return "success";}
}

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

在scopeController中:

@Controller
public class scopeController {@RequestMapping("/modelmap")public String testMM(ModelMap modelMap){modelMap.addAttribute("GetscopeObject","hello ModelMap");return "success";}
}

6.Model、ModelMap、Map三者之间的关系

二、向session域共享数据

在scopeController中:

@Controller
public class scopeController {@RequestMapping("/testsession")public String testMM(HttpSession session){session.setAttribute("testSession","hello session");return "success";}
}

在success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${session.testSession}"></p>
</body>
</html>

三、向application域共享数据

在scopeController中:

@Controller
public class scopeController {@RequestMapping("/application")public String testap(HttpSession session){ServletContext application= session.getServletContext();application.setAttribute("testapplication","hello,servletcontext=application");return "success";}
}

success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${application.testapplication}"></p>
</body>
</html>

四、SpringMvc的视图

1.ThymeleafView

在viewController类中:

package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class viewController {@RequestMapping("/view")public String view(){return "view";}//超链接的路径@RequestMapping("/testview")public String testview(){return "hi";}
}

 view.html:

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
</body>
</html>

hi.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
你好
</body>
</html>

2.转发视图

在viewController类中:

package com.pon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class viewController {@RequestMapping("/view")public String view(){return "view";}@RequestMapping("/testview")public String testview(){return "hi";}@RequestMapping("/testforword")public String testfoeword(){return "forward:/testview";}
}

view.html:

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
<a th:href="@{/testforword}">转发视图</a>
</body>
</html>

hi.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
你好
</body>
</html>

3.重定向视图

在viewController类中:

@Controller
public class viewController {@RequestMapping("/view")public String view(){return "view";}@RequestMapping("/testview")public String testview(){return "hi";}@RequestMapping("/testforword")public String testfoeword(){return "forward:/testview";}@RequestMapping("/testredirect")public String testRedirect(){return "redirect:/testview";}
}

 view.html:

<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/testview}">ThyleleafView</a>
<a th:href="@{/testforword}">转发视图</a>
<a th:href="@{/testredirect}">重定向视图</a>
</body>
</html>


4.视图控制器view-controller

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

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

相关文章

后台管理系统登录业务分析(图片验证码登录)

文章目录 1、登录业务分析2、登录开发流程2.1、获取图片验证码接口业务2.2、CodeImgServiceImpl2.2.1、响应 2.3、IndexController2.4、SysUserServiceImpl2.5、SysUserMapper.xml 3、springmvc拦截器创建&注册3.1、springmvc拦截器的创建3.2、springmvc拦截器注册3.3、Sys…

【Oracle APEX开发小技巧1】转换类型实现显示小数点前的 0 以 及常见类型转换

在 apex 交互式式网格中&#xff0c;有一数值类型为 NUMBER&#xff0c;保留小数点后两位的项&#xff0c;在 展示时小数点前的 0 不显示。 效果如下&#xff1a; 转换前&#xff1a; m.WEIGHT_COEFFICIENT 解决方案&#xff1a; 将 NUMBER&#xff08;20&#xff0c;2&#xf…

Vue 自定义ElementUI的Loading效果

import { loadingText, messageDuration } from "/settings";import { Loading } from "element-ui"; // loadingText、messageDuration 这两个参数我是调的公共配置文件,按自己需求来 const install (Vue, opts {}) > {/* 全局多彩Loading加载层 *…

cpolar:通过脚本自动更新主机名称和端口号进行内网穿透【免费版】

cpolar 的免费版经常会重新分配 HostName 和 Port&#xff0c;总是手动修改太过麻烦&#xff0c;分享一下自动更新配置文件并进行内网穿透的方法。 文章目录 配置 ssh config编写脚本获取 csrf_token打开登陆界面SafariChrome 设置别名 假设你已经配置好了服务器端的 cpolar。 …

Spring AI 调用 openAI 进行语音识别

Spring AI支持语音识别功能&#xff0c;目前仅支持OpenAI的Transcription模型。 项目搭建&#xff1a; 参考 Spring AI 介绍以及与 Spring Boot 项目整合 源码示例&#xff1a; RestController RequestMapping("/openai") public class OpenAiIAudioTranscriptionC…

1964springboot VUE 智慧社区可视化平台系统开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 springboot VUE社区可视化平台系统是一套完善的完整信息管理类型系统&#xff0c;结合springboot框架和VUE完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用springboot框架&#xff08;MVC模式开发&#xff09;&#xff0c;系统具有完整的源代码…

Android Studio main,xml 视图代码转换

Android Studio main,xml 视图&&代码转换 其实很简单,但是对我们小白来说还是比较蒙的。 废话不多说,直接上图。 我的Android Studio 是 4.0 版的 我刚打开是这个界面,在我想学习如何用代码来布局,可能大家也会找不见代码的位置。 follow me 是不是感觉很简单呢。…

【usb设备端口异常】——使用ls /dev/video*查看设备号时出现报错:ls:无法访问‘/dev/video*‘: 没有那个文件或目录

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、ls:无法访问/dev/video*: 没有那个文件或目录1. 问题描述2. 原因分析3. 解决方法 总结 前言 一、ls:无法访问’/dev/video*: 没有那个文件或目录 使用的这…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 披萨大作战(100分) - 三语言AC题解(Python/Java/Cpp)

&#x1f36d; 大家好这里是清隆学长 &#xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 &#x1f4bb; ACM银牌&#x1f948;| 多次AK大厂笔试 &#xff5c; 编程一对一辅导 &#x1f44f; 感谢大家的订阅➕ 和 喜欢&#x1f497; &#x1f…

《梦醒蝶飞:释放Excel函数与公式的力量》4.1if函数

第4章&#xff1a;逻辑与条件函数 第一节4.1 if函数 在Excel中&#xff0c;逻辑函数用于处理基于特定条件的真假判断&#xff0c;它们是构建复杂公式和进行高级数据分析的基础。本章将深入探讨逻辑函数的使用方法&#xff0c;特别是IF函数&#xff0c;这是Excel中最为常用的条…

1 UC

1 UC 1、环境变量2、环境变量表3、错误处理4、库文件4.1 静态库4.2 动态库4.3 动态库的动态加载 5、虚拟地址 1、环境变量 什么是环境变量&#xff1f; 每个进程都有一张自己的环境变量表&#xff0c;表中的每个条目都是形如“键值”形式的环境变量。进程可以通过环境变量访问…

计算机毕业设计Python深度学习房价预测 房价可视化 链家爬虫 房源爬虫 房源可视化 卷积神经网络 大数据毕业设计 机器学习 人工智能 AI

基于python一/二手房数据爬虫分析预测系统可视化 商品房数据Flask框架&#xff08;附源码&#xff09; 项目介绍python语言、Flask框架、MySQL数据库、Echarts可视化 sklearn机器学习 多元线性回归预测模型、requests爬虫框架 链家一手房 一手房数据商品房数据、分析可视化预测…

前后端分离的后台管理系统源码,快速开发OA、CMS网站后台管理、毕业设计项目

那有没有一款软件解-决这种现状呢?答案是肯定的。引入我们的软件——eladmin。 介绍 ELADMIN,一个简单且易上手的 Spring boot 后台管理框架,已发布 Mybatis-Plus 版本,为开发者提供了一个全-面、高-效的解-决方案。 特点 高-效率:前后端完全分离,项目简单可配,内置代码…

在windows和linux服务器之间互传文件

最近需要在windows上和linux服务器上实现相互传送文件&#xff0c;作为一个linux小白&#xff0c;研究了一个上午&#xff0c;终于成功了&#xff0c;记录一下。 使用的是SFTP方式。 Linux服务器系统是 Ubuntu 18.04 64位&#xff0c;windows 是 win10专业版。 首先在Linux服…

http和https的区别在哪

HTTP&#xff08;超文本传输协议&#xff09;和HTTPS&#xff08;超文本传输安全协议&#xff09;之间存在几个关键区别主要涉及安全性、端口、成本、加密方式、搜索引擎优化&#xff08;SEO&#xff09;、身份验证等方面 1、安全性&#xff1a;HTTP&#xff08;超文本传输协议…

Web前端项目-页面动态背景【附完整源码】

页面动态背景 一&#xff1a;花瓣背景 页面效果&#xff1a; HTML代码 <!DOCTYPE HTML> <HTML> <TITLE>花瓣漫舞</TITLE> <META NAME"Generator" CONTENT"EditPlus"> <META NAME"Author" CONTENT"&quo…

区块链技术:探索7个物联网应用的潜力

在当今数字化时代&#xff0c;区块链技术逐渐成为一种受到广泛关注的技术创新。本文将以《区块链技术&#xff1a;探索7个物联网应用的潜力》为题&#xff0c;介绍区块链技术在物联网领域的七个应用潜力。物联网作为未来发展的重要方向&#xff0c;结合区块链技术的应用将为我们…

【因果推断python】47_元学习1

目录 S-Learner 简单回顾一下&#xff0c;我们现在感兴趣的是发现治疗效果的异质性&#xff0c;即确定单位对治疗的不同反应。在这个框架中&#xff0c;我们想估计 或 在连续的情况下。换句话说&#xff0c;我们想知道这些单位对治疗的敏感程度。这在我们不能治疗所有人并且需…

Flink 1.19.1 standalone 集群模式部署及配置

flink 1.19起 conf/flink-conf.yaml 更改为新的 conf/config.yaml standalone集群: dev001、dev002、dev003 config.yaml: jobmanager address 统一使用 dev001&#xff0c;bind-port 统一改成 0.0.0.0&#xff0c;taskmanager address 分别更改为dev所在host dev001 config.…

【SD3的Turbo也来了】Jasper AI用Flash Diffusion的蒸馏技术为SD3提速

Flash Diffusion 是一种新颖的图像生成方法&#xff0c;旨在显著提高现有条件扩散模型&#xff08;例如教师模型&#xff09;的速度&#xff0c;而无需牺牲性能。它通过引入以下技术来实现这一点&#xff1a; 蒸馏损失&#xff1a;在教师模型和学生模型之间引入蒸馏损失&#…