首页 > 其他分享 >SpringBoot-04-thymeleaf模板

SpringBoot-04-thymeleaf模板

时间:2023-01-10 18:55:05浏览次数:57  
标签:SpringBoot 04 Controller getValue thymeleaf html model 模板

目录


前言

参考自:https://www.cnblogs.com/hellokuangshen/p/12516870.html


一、什么是模板引擎?

模板引擎是为了使用户界面与业务数据分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的文档。

JSP就是一个模板引擎。

JSP有什么特点呢?

  • jsp文件允许同时使用html和java语言编写并生成动态网页。

故我们可以总结一下,模板引擎就是

  • 使用符合特定规范的格式文本
  • 生成特定格式的文档(如html)

那么为什么要学thymeleaf模板呢?

  1. SpringBoot以jar的方式打包项目,不是war;
  2. SpringBoot使用嵌入式的Tomcat,默认不支持jsp。

二、引入thymeleaf依赖

使用Maven,添加依赖

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

如果在html文件中导入命名空间的约束,编写代码的时候会提示补全,方便我们使用。

xmlns:th="http://www.thymeleaf.org"
<html lang="en" xmlns:th="http://www.thymeleaf.org">

三、thymeleaf语法

所有的html元素都可以被thymeleaf替换接管,用th :元素名,如th:text=""

1.基础功能:传值取值

thymeleaf模板如何接收controller传递的值?使用Model

第一步,新建GetValue.html
使用<div th:text="${msg}"></div>接收并展示Controller层传递的数据。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>getValue</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>

第二步,新建Controller,
使用model.addAttribute("msg","getValue");传值给View层。

@Controller
@RequestMapping("/helloThymeleaf")
public class helloThymeleaf {
    @RequestMapping("/getValue")
    public String getValue_html(Model model){
        model.addAttribute("msg","getValue");
        return "getValue";
    }
}

在这里插入图片描述

运行Application类,访问链接http://localhost:8080/helloThymeleaf/getValue,效果图如下:
在这里插入图片描述
注意:Session和Model的区别

区别 1. session 里放的数据可以在其他页面使用
区别 2. model的数据,只能在接下来的页面使用,其他页面就不能使用

2.转义字符串为标签

如果我们传递的msg为<h1>getValue</h1>这种带有html标签的字符串,则该字符串有两种解释方法:

1.显示为文本<h1>getValue</h1>
2.显示为标题一格式的getValue

如何使传递的字符串<h1>被认为是标签呢?只需要使用th:utext="${msg}"即可。

修改getValue.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>getValue</title>
</head>
<body>
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
</body>
</html>

修改Controller类helloThymeleaf.java

@Controller
@RequestMapping("/helloThymeleaf")
public class helloThymeleaf {
    @RequestMapping("/getValue")
    public String getValue_html(Model model){
        model.addAttribute("msg","<h1>getValue</h1>");
        return "getValue";
    }
}

运行后显示
在这里插入图片描述

3.for-each遍历功能

Controller层发送一个List

model.addAttribute("pets", Arrays.asList("cat","dog","pig"));

Arrays.asList("cat","dog","pig")该方法是将数组转化成List集合的方法,用此方法得到的List的长度是不可改变的。

View层遍历这个List

<ol>
    <li th:each="pet:${pets}" th:text="${pet}"></li>
</ol>

效果图
在这里插入图片描述

标签:SpringBoot,04,Controller,getValue,thymeleaf,html,model,模板
From: https://www.cnblogs.com/cnleika/p/17041165.html

相关文章

  • 【转】pageOffice插件 springboot实现服务器上Word文档在线打开编辑保存
    pageOffice插件springboot实现服务器上Word文档在线打开编辑保存需求:在oa系统上,想实现在线,服务器上doc,docx文档,在web打开,编辑。编辑后,可以再同步保存到服务器端。开发......
  • 04-VGG16 图像分类
       图1VGG的网络结构  图2VGG16的网络 VGG网络结构的理解,参考:https://blog.csdn.net/Keep_Trying_Go/article/details/123943751Cifar10的vgg16网络pyto......
  • Springboot:拦截器和过滤器
    项目的开发中,在某些情况下,我们需要对客户端发出的请求进行拦截,常用的API拦截方式有Fliter,Interceptor,ControllerAdvice以及Aspect。请求从Filter-->>Controller的过程中,只......
  • Springboot .properties或.yml配置文件读取pom.xml文件值
    需要在pom.xml中打开过滤才可以<build><resources><resource><directory>src/main/resources</directory><fi......
  • springboot启动报错
    前言​ 学习构建springboot+maven项目,在父模块下创建的一个新的模块,依赖版本由父模块pom.xml控制;写了一个测试接口,启动测试时,一直报错,初始化失败、datasource创建失败、......
  • springboot集成dubbo
    使用SpringBoot+Dubbo搭建一个简单分布式服务实战之前,先来看几个重要的概念什么是分布式?什么是Duboo?Dubbo架构什么是RPC?为什么要用Dubbo?开始实战1:zoo......
  • springboot_yaml与properties区别
    application.properties##springboot这个配置文件可以配置什么的东西呢?##第一种:背官方的配置,呜呜呜太多了##然后,官方推荐视图.yaml代替.properties#key=value,......
  • SpringBoot设置跨域的几种方式
    什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名、端口、协议任一不同,都是跨域 原因:由于浏览器的同源策略,即a网站只能访问a网站的内容,......
  • springboot集成caffine本地缓存
    一、缓存算法1.FIFOFIFO(FirstinFirstout)先进先出。可以理解为是一种类似队列的算法实现算法:当一个查询请求命中了某个元素之后,便会将它放入到队列中,后续的命中元素也......
  • 适合编程初学者的开源项目:小游戏2048(微信小程序版)
    目标为编程初学者打造入门学习项目,使用各种主流编程语言来实现。2048游戏规则一共16个单元格,初始时由2或者4构成。1、手指向一个方向滑动,所有格子会向那个方向运动。2......