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.mfbz.cn/a/728727.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

BirdTalk IM集群中消息流转策略讨论

BirdTalk IM集群中消息流转策略讨论 目前群聊的存储策略是1写多读方案&#xff1b;每个群组一个队列&#xff0c;按时间顺序排列&#xff0c;不区分用户&#xff1b; 私聊的存储是写扩散的&#xff0c;每个人都有自己的消息队列&#xff0c;按时间顺序 保存所有的消息&#x…

贴图大师(Model Painter) 下载使用

贴图大师(Model Painter)是一款是一款专业的纹理贴图工具&#xff0c;支持激光扫描的数据模型和通过摄影测量建模的模型&#xff0c;功能强大&#xff0c;操作简单。 贴图大师是一款专门解决三维建模数字化中纹理编辑和优化的软件工具。贴图大师的输入模型可以支持激光扫描的数…

HarmonyOS角落里的知识:一杯冰美式的时间 -- 之打字机

一、前言 模拟编辑器或者模拟输入框中文字啪啦啪啦输入的效果&#xff0c;往往能够吸引人们的眼球&#xff0c;让用户的注意力聚焦在输入的内容上&#xff0c;本文将和大家探讨打字机效果的实现方式以及应用。Demo基于API12。 二、思路 拆分开来很简单&#xff0c;将字符串拆…

SAP PP学习笔记23 - 生产订单(制造指图)的元素2 - 决济规则(结算规则)

上一章讲了生产订单&#xff08;制造指图&#xff09;画面的基本元素。 SAP PP学习笔记22 - 生产订单&#xff08;制造指图&#xff09;的元素1-CSDN博客 本章继续讲生产订单上面的其他元素。 1&#xff0c;Settlement rule&#xff08;决济规则(结算规则)&#xff09;概要 M…

2024-6-20 Windows AndroidStudio SDK(首次加载)基础配置,SDK选项无法勾选,以及下载失败的一些解决方法

2024-6-20 Windows AndroidStudio SDK(首次加载)基础配置,SDK选项无法勾选,以及下载失败的一些解决方法 注意:仅仅是SDK这种刚安装时的配置的下载,不要和开源库的镜像源扯到一起&#xff01;&#xff01;&#xff01;&#xff01; 最近想玩AndroidStudio的JNI开发, 想着安装后…

02_02_SpringMVC基于注解的应用

一、请求处理 1、常用注解 RequestMapping 作用&#xff1a;用来匹配客户端发送的请求&#xff08;用来处理URL映射&#xff0c;将请求映射到处理方法中&#xff09;&#xff0c;可以在类或者方法上使用。 用在类上&#xff0c;可以将请求模块化&#xff0c;避免请求方法中的…

Linux命令的进程关系

一、shell简述 shell是一个命令行解释器工具&#xff0c;它是一个时刻都在运行的程序&#xff0c;当我们在命令行输入命令&#xff0c;shell会去解释执行这个命令。 shell这个工具不止一种&#xff0c;我们使用Linux系统的时候&#xff0c;默认启动的shell 是/etc/passwd 这个…

超级好用的JSON格式化可视化在线工具

JSON是开发非常常用的一种报文格式&#xff0c;最常见的需求就是将JSON进行格式化&#xff0c;最好是有图形化界面显示结构关系&#xff0c;以便进行数据分析。 理想的在线JSON工具&#xff0c;应该支持快速格式化、可压缩、快捷复制、可下载导出&#xff0c;对存在语法错误的地…

【五】【QT开发应用】C++中lambda表达式,值捕获,引用捕获,隐式捕获,lambda表达式的返回类型

Lambda表达式 复盘 Lambda表达式 Lambda 表达式是 C11 引入的一种特性&#xff0c;用于定义匿名函数。它使得可以在代码中方便地定义和使用小段函数&#xff0c;而无需专门定义一个命名的函数。这在需要传递函数作为参数或者需要定义内联函数时非常有用。 基本语法 基本语法…

Apple - Core Text Programming Guide

本文翻译整理自&#xff1a;Core Text Programming Guide&#xff08;Updated: 2014-09-17 https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005533 文…

docker将容器打包提交为镜像,再打包成tar包

将容器打包成镜像可以通过以下步骤来实现。这里以 Docker 为例&#xff0c;假设你已经安装了 Docker 并且有一个正在运行的容器。 1. 找到正在运行的容器 首先&#xff0c;你需要找到你想要打包成镜像的容器的 ID 或者名字。可以使用以下命令查看所有正在运行的容器&#xff…

高速异地组网怎么办理?

在当今信息化时代&#xff0c;跨地域的远程办公、远程教育、远程医疗等需求越来越多。而高速异地组网作为一种解决不同地区之间快速组建局域网的方法&#xff0c;被广泛应用。本文将介绍一款异地组网内网穿透产品——【天联】&#xff0c;并提供其办理流程。 【天联】组网是什…

【系统设计】如何权衡范式与反范式设计

一、什么是范式设计与反范式设计 1.1、范式设计&#xff08;Normalization&#xff09; 定义&#xff1a; 范式设计是数据库设计中最基础的设计原则之一&#xff0c;它主要通过规范化数据模型&#xff0c;减少数据冗余和数据不一致的问题。 常用的范式&#xff1a; 第一范式…

Android Studio main,xml 视图代码转换

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

基于DE2-115平台的VGA显示实验

一.任务需求 深入了解VGA协议&#xff0c;理解不同显示模式下的VGA控制时序参数&#xff08;行频、场频、水平/垂直同步时钟周期、显示后沿/前沿等概念和计算方式&#xff09;&#xff1b;通过Verilog编程&#xff0c;在至少2种显示模式下&#xff08;64048060Hz,102476875Hz&…

Day14——Python文本挖掘数据分析

文章目录 竞争分析-品类分布-适用对象竞争分析-产品结构-拜耳在这里插入图片描述竞争分析-产品结构-拜耳-BCG图竞争分析-产品结构-拜耳-明星竞争分析-产品结构-拜耳-奶牛竞争分析-产品结构-拜耳-问题竞争分析-产品结构-安速-BCG图竞争分析-产品结构-安速-明星竞争分析-产品结构…

vue3项目使用Electron打包成exe的方法与打包报错解决

将vue3项目打包成exe文件方法 一、安装 1.安装electron npm install electron --save-devnpm install electron-builder --save-dev 2.在vue项目根目录新建文件index.js // index.js// Modules to control application life and create native browser window const { app…

Python日志管理利器:如何高效管理平台日志

一、为什么需要日志管理&#xff1f; 日志是应用程序的重要组成部分&#xff0c;它记录了应用程序的运行状态、错误信息以及用户交互等关键信息。良好的日志管理可以帮助开发人员及时发现和解决问题&#xff0c;提高应用程序的稳定性和可靠性。 项目在本地开发调试时&#xf…

AGI的多模态融合

在人工智能的宏伟蓝图中&#xff0c;人工通用智能&#xff08;AGI&#xff09;代表着一个集大成者&#xff0c;一个能够理解、学习、适应并执行任何智能任务的系统。随着我们对AGI的探索愈发深入&#xff0c;尤其是在视觉、语言和其他模态的融合上&#xff0c;关于AGI的讨论愈发…

详解DAC数模转换+DAC输出模拟电压的测量比对实验程序

前言&#xff1a;详解DAC数模转换原理DAC输出模拟电压的测量比对实验程序&#xff08;使用 DAC 通道 1 输出模拟电压&#xff0c;然后通过 ADC1 的通道 1 对该输出电压进行读取&#xff0c;并显示在 LCD 模块上面&#xff0c;DAC 的输出电压可以通过按键&#xff08;或 USMART&…