什么是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. 傻瓜式搭建私人网络硬盘——owncloud安装指南

    百度云这个贱货天天删我资源,我已经忍无可忍了,于是想搭建一个owncloud来放我的里番.使用owncloud不仅安全,而且还可以在线播放,离线下载,功能相当强大. 然而·····网上查了一下,竟然无 ...

  2. kubernetes 简单yaml文件运行例子deployment

    运行一个deployment: kubectl  run  nginx-deployment  --image=nginx:1.7.9  --replicas=2 基本例子: nginx-test.y ...

  3. BZOJ4873[Shoi2017]寿司餐厅——最大权闭合子图

    题目描述 Kiana最近喜欢到一家非常美味的寿司餐厅用餐.每天晚上,这家餐厅都会按顺序提供n种寿司,第i种寿司有一个 代号ai和美味度di,i,不同种类的寿司有可能使用相同的代号.每种寿司的份数都是无 ...

  4. 网页调起QQ聊天

    将QQ账号换成正常的QQ号即可,要确保这个QQ支持临时会话 <a href="http://wpa.qq.com/msgrd?v=3&uin=QQ账号&site=qq& ...

  5. 【宝塔linux】 导入mysql 大文件失败的问题

    导入数据库有四种方法 1.宝塔网站自带的数据库导入 2.phpmyadmin导入 3.远程到linux服务器用导入命令 使用xshell进入到控制台 1.首先建空数据库 mysql>create ...

  6. Centos install Python3

    下载Python3wget http://python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz 安装yum install openssl-devel bzi ...

  7. Centos Install Keepalived

    Keepalived简介Keepalived 的作用是检测 web 服务器的状态,如果有一台 web 服务器死机,或工作出现故障,Keepalived 将检测到,并将有故障的 web 服务器从系统中剔 ...

  8. Python面试指南

    1.Python基本语法 1.@staticmethod 和 @classmethod Python中有三种方法,实例方法.类方法(@classmethod).静态方法(@staticmethod). ...

  9. Codeforces Round #493 (Div. 2)

    C - Convert to Ones 给你一个01串 x是反转任意子串的代价 y是将子串全部取相反的代价 问全部变成1的最小代价 两种可能 一种把1全部放到一边 然后把剩下的0变成1  要么把所有的 ...

  10. OpenLayers学习笔记(二)— QML与HTML通信之画图

    作者: 狐狸家的鱼 Github: 八至 本文链接:QML与 HTML通信—实现QML中点击功能按钮在地图上画图 一.HTML-map var drarGraphic; var drawType;fu ...