什么是thymeleaf?

thymeleaf是一个模板引擎,是用来在Spring Boot中代替JSP的

  1. 引用thymeleaf

            <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  2. 控制thymeleaf的版本号(thymelaf3以上的话,layout必须2以上才支持)

    <properties>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

创建最简单的thymeleaf

thymeleaf默认的路径是classpath://templates,必须是这个路径才能识别

我们在templates文件夹下面新建一个Vae.html,然后在Controller里面写一个方法去调用这个html

    @RequestMapping("/Vae")
public String thymeleafTest(){
return "Vae";
}

点击运行,如图:

完美,我们已经新建了一个最简单的thymeleaf了

thymeleaf语法

可以去官网下载文档查看

  1. 写一个hello传到html,我的Controller改为这样
    @RequestMapping("/Vae")
public String thymeleafTest(Map<String,Object> map){
map.put("hello","你好");
return "Vae";
}

前端的html需要做两个地方

  1. 引入thymeleaf的声明,这样写thymeleaf就会有提示
<html xmlns:th="http://www.thymeleaf.org">
  1. 用thymeleaf
<div th:text="${hello}">欢迎</div>

写完之后,整个前端html就是这样

<html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1>thymeleaf许嵩</h1> <div th:text="${hello}">欢迎</div>
</body>
</html>

重启,再来访问,如图:

非常好,写th:text后面的${},让我想起来了JSP里面的EL。可以发现我

里面用了thymeleaf,将会得到“你好”二字,如果我们将这个html拷贝到项目外面会,显示的就是欢迎了,这表明了一件事

含有thymeleaf语法的html文件只有经过thymeleaf模板解析之后生效,项目之外就变成一个普通的HTML了

  1. th可以替换任意的HTML元素
<div id="${hello}" class="${hello}" th:text="${hello}"></div>
  1. thymeleaf表达式
${}:这个我们已经用过了,就是取值的
*{}:这个是代指${},例如Person类有name属性,${Person.name}=*{name}
#{}:获取国际化内容
@{}:定义url的,例如 @{https://localhost:8080(id=${Id})}
~{}:片段引用表达式

Spring Boot笔记六:Thymeleaf介绍的更多相关文章

  1. Spring Boot笔记(六) springboot 集成 timer 定时任务

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...

  2. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  3. Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

    Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ...

  4. Spring Boot(十六):使用Jenkins部署Spring Boot

    Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ...

  5. spring boot 笔记--第三章

    spring boot 笔记 第三章,使用Spring boot 构建系统: 强烈建议支持依赖管理的构建系统,Maven或Gradle 依赖管理: Spring Boot的每版本都会提供它支持的依赖列 ...

  6. spring boot / cloud (六) 开启CORS跨域访问

    spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...

  7. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  8. Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...

  9. WebFlux 集成 Thymeleaf 、 Mongodb 实践 - Spring Boot(六)

    这是泥瓦匠的第105篇原创 文章工程: JDK 1.8 Maven 3.5.2 Spring Boot 2.1.3.RELEASE 工程名:springboot-webflux-5-thymeleaf ...

随机推荐

  1. fpm 打包教程

    常用yum命令: Yum安装时需要安装到指定的文件夹,则需要 --installroot yum install --installroot=/usr/src/ vim 常用rpm命令: 常用yum仓 ...

  2. FPGA笔试题集锦(血的教训)

    1.名词解释: FPGA:现场可编程门阵列,一般工艺SRAM(易失性),所以要外挂配置芯片. CPLD:复杂可编程逻辑器件,一般工艺Flash(不易失). ASIC:专用集成电路 SOC:片上系统 S ...

  3. 【XSY2707】snow 线段树 并查集

    题目描述 有\(n\)个人和一条长度为\(t\)的线段,每个人还有一个工作范围(是一个区间).最开始整条线段都是白的.定义每个人的工作长度是这个人的工作范围中白色部分的长度(会随着线段改变而改变).每 ...

  4. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...

  5. Linux qemu-nbd mount qemu disk image

    Linux qemu-nbd mount qemu disk image deepin@deepin:~$ deepin@deepin:~$ qemu-nbd --help Usage: qemu-n ...

  6. word 2013 题注、图注、插入图片自动修改大小、批量更新题注编号

    1 .题注 图片下面的文字说明,如  图 1.1.1 2.图注 图的标题格式,可以右键修改段落为居中,选中图片,点下此格式快捷居中等其他格式 3. 题注插入 效果 如下 4.题注自动居中对齐 先点击图 ...

  7. Codeforces | CF1037D 【Valid BFS?】

    题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...

  8. debugger

    今天爬取cfda时遇到的困难,一旦开启了调试,就debugger pause, ???还有这种操作 一顿google,百度,解决了这个问题,点一下Deactivate breakPoints,然后点一 ...

  9. 单片机pwm控制基本原理详解

    前言 PWM是Pulse Width Modulation的缩写,它的中文名字是脉冲宽度调制,一种说法是它利用微处理器的数字输出来对模拟电路进行控制的一种有效的技术,其实就是使用数字信号达到一个模拟信 ...

  10. MYSQL主从复制制作配置方案

    1. 主从复制机器配置 操作系统:centos7 x64 基于vagrant下的virtual box的虚拟机两台 master ip:192.168.21.11, slave ip 192.168. ...