本文参考:https://developer.aliyun.com/article/769977
- thymeleaf的优点:实现动静分离,常规的编程使用jsp,jsp页面你要想看到页面效果,布局样式有没有问题就必须运行程序去访问url才能看到页面,而引入thymeleaf之后可以实现html文件直接查看布局,样式有没有问题。
- thymeleaf实现:在html页面中加入
<html xmlns:th="http://www.thymeleaf.org">
声明,并且配置pom.xml文件配置依赖 - springboot默认thymeleaf的html文件存放在templates(模板)下
- 了解一些thymeleaf的语法:${}model取值
- 可通过配置项配置thymeleaf,开发注意事项:要禁用thymeleaf的页面缓存,不然可能浏览器html不是新的
依赖添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
基本语法
<div th:th="${name}"></div>
从model当中取名为name的值
<h3 th:text="'PID:202091100'+${photo.id}"></h3>
灵活使用
<a th:href="@{/lookPhoto/}+${photo.id}">
<img th:src="@{${photo.address}}" alt="img">
</a>
链接跳转
引入css
<link rel="stylesheet" th:href="@{index.css}">
引入JavaScript:
<script type="text/javascript" th:src="@{index.js}"></script>
model存入map
<h2>Map取值</h2>
<table bgcolor="#8fbc8f" border="1">
<tr>
<td>place:</td>
<td th:text="${map.get('place')}"></td>
</tr>
<tr>
<td>feeling:</td>
<td th:text="${map['feeling']}"></td>
</tr>
</table>
消息表达式:#{…}
if判断:参考https://blog.csdn.net/weixin_38192427/article/details/108061869
控制是否显示
th:if="true"
th:if="${value}"
基本配置
禁用缓存这对开发来说很重要,配置编写在application.yaml文件当中