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

news2024/10/6 20:36:53

一、向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.coloradmin.cn/o/1831414.html

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

相关文章

【UE5.1】制作自己的载具

目录 前言 效果 步骤 一、制作载具模型 二、载具设置 三、控制载具 前言 在前面我们通过UE4完成了载具的制作&#xff0c;下面我们介绍一下如何通过UE5制作载具。 效果 步骤 一、制作载具模型 制作方法同【UE4 制作自己的载具】1-使用3dsmax制作载具 二、载具设置 …

高考杂志高考杂志社高考编辑部2024年第14期目录

高考论坛 新高考背景下优化高中数学教学方法探究 韩玉新; 3-5 基于高考评价体系的高中历史大单元复习模式建构 钱敏杰; 6-8 新高考背景下高中语文课堂优化作业设计策略 吴丽容; 9-11《高考》投稿&#xff1a;cn7kantougao163.com 新高考视域下高中地理课堂促进…

别太小看“静态免杀“

0x01 简述 免杀总体来说可分为两种&#xff0c;静态免杀/动态免杀。往往来说&#xff0c;我们更注重于在内部代码层面实现一些免杀技巧&#xff0c;但在有些时候&#xff0c;动态免杀静态免杀以"打组合拳"的方式效果往往会更出人所料。 当我们的程序生成后&#xf…

高考志愿填报和未来的职业规划

高考成绩出来那一刻&#xff0c;我们就站在了人生的岔路口上&#xff0c;面临这不同的选择&#xff0c;走不同的路线、过不同的生活...... 除了成绩会决定一个人的未来走向之外&#xff0c;报考的专业和学校影响也是终身。高考志愿填报和未来职业规划应该息息相关&#xff0c;…

python图像处理库-PIL(Pillow)

PIL库全称为Python Imaging Library&#xff0c;即Python图像处理库&#xff0c;是一个在Python中用于处理图像的非常流行的库。 一、PIL介绍 这个库提供了广泛的文件格式支持、高效的内部表示以及相当强大的图像处理功能。 核心图像库旨在快速访问存储在几种基本像素格式中的数…

【C++】模板进阶(特化)

&#x1f308;个人主页&#xff1a;秦jh_-CSDN博客&#x1f525; 系列专栏&#xff1a;https://blog.csdn.net/qinjh_/category_12575764.html?spm1001.2014.3001.5482 目录 非类型模板参数 数组越界检查 按需实例化 模板的特化 函数模板特化 类模板特化 全特化 ​…

【网络编程开发】17.“自动云同步“项目实践

17."自动云同步"项目实践 文章目录 17."自动云同步"项目实践项目简介功能需求需求分析实现步骤 1.实现TCP通信server.c 服务端tcp.hclient.c 客户端 函数封装tcp.ctcp.hserver.cclient.c编译运行 2.实现文件传输sever.cclient.ctcp.ctcp.hMakeifle编译运行…

死锁预防之银行家算法

死锁预防之银行家算法 第一章 概述 Dijkstra提出了一种能够避免死锁的调度算法,称为银行家算法。 它的模型基于一个小城镇的银行家,他向一群客户分别承诺了一定的贷款额度,每个客户都有一个贷款额度,银行家知道不可能所有客户同时都需要最大贷款额,所以他只保留一定单位…

wps 二维数据转转一维度数据

HSTACK(TOCOL(C2:H2&A3:A8),TOCOL(B3:B8&C1:H1),TOCOL(C3:H8))

------构造类型数据—结构体---- + ----函数-----

构造类型数据——结构体 1&#xff09;结构体的基本概念 结构体&#xff08;struct&#xff09;是C语言&#xff08;以及其他一些编程语言&#xff09;中用于将不同类型的数据组合成一个单一类型的方式。这种数据类型允许你将多个变量&#xff08;可能是不同类型&#xff09;…

iconfont的使用(超简单)

iconfont的使用&#xff08;超简单&#xff09; 1、iconfont 是什么&#xff1f;2、使用2.1、新建项目2.2、搜图标 添加 至项目中2.3、下载iconfont的包文件![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/91a0a07cd4b74798b7fb333dddca7724.png)2.4、画一个文件夹…

基于Python的花卉识别分类系统【W9】

简介&#xff1a; 基于Python的花卉识别分类系统利用深度学习和计算机视觉技术&#xff0c;能够准确识别和分类各种花卉&#xff0c;如玫瑰、郁金香和向日葵等。这种系统不仅有助于植物学研究和园艺管理&#xff0c;还在生态保护、智能农业和市场销售等领域展现广泛应用前景。随…

springboot原理篇-bean管理

springboot原理篇-bean管理&#xff08;二&#xff09; 我们今天主要学习IOC容器中Bean的其他使用细节&#xff0c;主要学习以下三方面&#xff1a; 如何从IOC容器中手动的获取到bean对象bean的作用域配置管理第三方的bean对象 一、获取Bean 了解即可&#xff0c;默认情况下…

将Vite添加到您现有的Web应用程序

Vite&#xff08;发音为“veet”&#xff09;是一个新的JavaScript绑定器。它包括电池&#xff0c;几乎不需要任何配置即可使用&#xff0c;并包括大量配置选项。哦——而且速度很快。速度快得令人难以置信。 本文将介绍将现有项目转换为Vite的过程。我们将介绍别名、填充webp…

Springboot整合阿里云ONS RocketMq(4.0 http)

1. 引入依赖 <!--阿里云ons&#xff0c;方便的接入到云服务--> <dependency><groupId>com.aliyun.openservices</groupId><artifactId>ons-client</artifactId><version>1.8.4.Final</version> </dependency>2. 配置 配…

牛客周赛 46 F 祥子拆团

原题链接&#xff1a;F-祥子拆团 题目大意&#xff1a;多测&#xff0c;每次给a,b&#xff0c;要将a分解成b个数相乘&#xff0c;问有多少种分的方法。 思路&#xff1a;对a进行质因数分解&#xff0c;对每一个质数计数&#xff0c;然后分到b个篮子里面&#xff0c;允许篮子里…

SFNC —— 图像格式控制(三)

系列文章目录 SFNC —— 标准特征命名约定&#xff08;一&#xff09; SFNC —— 设备控制&#xff08;二&#xff09; SFNC —— 图像格式控制&#xff08;三&#xff09; 文章目录 系列文章目录4、图像格式控制&#xff08;Image Format Control&#xff09;1. 图像格式控制&…

Flink系列之:Generating Watermarks生成水印

Flink系列之&#xff1a;Generating Watermarks生成水印 一、水印策略简介二、使用水印策略三、处理闲置资源四、水印对齐五、编写水印生成器六、编写周期性水印生成器七、编写标点水印生成器八、水印策略和 Kafka 连接器九、Operators如何处理水印十、已弃用的AssignerWithPer…

电信网关配置管理系统 del_file.php 前台RCE漏洞复现

0x01 产品简介 中国电信集团有限公司(英文名称“China Telecom”、简称“中国电信”)成立于2000年9月,是中国特大型国有通信企业、上海世博会全球合作伙伴。电信网关配置管理系统是一个用于管理和配置电信网络中网关设备的软件系统。它可以帮助网络管理员实现对网关设备的远…