Spring Boot笔记六:Thymeleaf介绍
什么是thymeleaf?
thymeleaf是一个模板引擎,是用来在Spring Boot中代替JSP的
- 引用thymeleaf - <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 
- 控制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语法
可以去官网下载文档查看
- 写一个hello传到html,我的Controller改为这样
    @RequestMapping("/Vae")
    public String thymeleafTest(Map<String,Object> map){
        map.put("hello","你好");
        return "Vae";
    }
前端的html需要做两个地方
- 引入thymeleaf的声明,这样写thymeleaf就会有提示
<html xmlns:th="http://www.thymeleaf.org">
- 用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了
- th可以替换任意的HTML元素
<div id="${hello}" class="${hello}" th:text="${hello}"></div>
- thymeleaf表达式
${}:这个我们已经用过了,就是取值的
*{}:这个是代指${},例如Person类有name属性,${Person.name}=*{name}
#{}:获取国际化内容
@{}:定义url的,例如 @{https://localhost:8080(id=${Id})}
~{}:片段引用表达式
Spring Boot笔记六:Thymeleaf介绍的更多相关文章
- Spring Boot笔记(六) springboot 集成 timer 定时任务
		个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ... 
- spring boot 学习(二)spring boot 框架整合 thymeleaf
		spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ... 
- Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
		Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ... 
- Spring Boot(十六):使用Jenkins部署Spring Boot
		Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ... 
- spring boot 笔记--第三章
		spring boot 笔记 第三章,使用Spring boot 构建系统: 强烈建议支持依赖管理的构建系统,Maven或Gradle 依赖管理: Spring Boot的每版本都会提供它支持的依赖列 ... 
- spring boot / cloud (六) 开启CORS跨域访问
		spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ... 
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
		上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ... 
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
		本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ... 
- WebFlux 集成 Thymeleaf 、 Mongodb 实践 - Spring Boot(六)
		这是泥瓦匠的第105篇原创 文章工程: JDK 1.8 Maven 3.5.2 Spring Boot 2.1.3.RELEASE 工程名:springboot-webflux-5-thymeleaf ... 
随机推荐
- Vue 快速入门
			Vue框架介绍 中文文档: https://cn.vuejs.org/v2/guide/ Vue是一个构建数据驱动的web界面的渐进式框架. 目标是通过尽可能简单的API实现响应式的数据绑定和组合的视 ... 
- CSS初步学习
			1.选择器: 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. id 选择器 id 选择器可以为标有特定 id 的 ... 
- quartus prime 16.0 报警告 inferring latch
			前言 当写always组合逻辑块时,可能会写出 poor code.综合时软件会推断出锁存器.例如下面代码: always @* begin 'b1) begin w = (a & b) ^ ... 
- Java归并排序的递归与非递归实现
			该命题已有无数解释,备份修改后的代码 平均时间复杂度: O(NLogN) 以2为底 最好情况时间复杂度: O(NLogN) 最差情况时间复杂度: O(NLogN) 所需要额外空间: 递归:O(N + ... 
- Android 简单调用摄像头
			IDE: android studio3.1.2 界面: activity_main2.xml 
- Nowcoder | [题解-N189]牛客OI赛制测试赛3
			这场说实话确实水(逃*1),表示差一点就AK了(逃*2),然而被卡两个特判的我\(ssfd\)...\(qwq\) 表示这是第一次发整场比赛的题解...还请各位大佬原谅我太蒻写的垃圾啊\(qwq\). ... 
- C 头文件、宏、编译问题
			@2019-02-15 [小记] > C 头文件的防重复包含是针对同一个源文件而言 原因: #include 头文件就是一段代码的拷贝,头文件中若有类型定义等,重复包含就会造成编译错误,若无类型 ... 
- optimize PHP-FPM优化
			php-fpm进程pidpids=$(ps aux | grep ${process} | grep -v "grep" | awk '{print $2}') php-fpm 关 ... 
- Centos7之Gcc安装
			1下载Gcc包进行安装https://gcc.gnu.org 下载下来的是一个包文件,但是要进行一系列的编译安装,听麻烦的. 不如直接使用这个yum来从网络上进行安装. 2 yumyum instal ... 
- linux/mac下一键删除下载失败的maven jar包
			echo 正在搜索... find . -name "*lastUpdated" | xargs rm -fr echo 搜索完毕 
