# SpringBootThymeleaf
**Repository Path**: turing-ice/SpringBootThymeleaf
## Basic Information
- **Project Name**: SpringBootThymeleaf
- **Description**: 使用thymeleaf
- **Primary Language**: HTML
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-02-06
- **Last Updated**: 2026-06-05
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
使用thymeleaf
[TOC]
[csdn](https://blog.csdn.net/weixin_46396315/article/details/136061880)
# 介绍
------
**Thymeleaf**是一种新的**模板引擎**,完全可以代替传统的**JSP**,一个**html**页面效果做好后,只需在其需要后端控制变化的标签里添加属性,让**html**从静态页面变成动态页面,学习**thymeleaf**,只需要学习他的一些**标签**的**用法**和它**内嵌**的一些变量即可。顺带一说,学习过**vue**在**html**上的那些标签*[v-if、v-for...]*的,相对于会上手的快一些
# 使用
------
## 添加pom
------
想要在springboot上使用thymeleaf,需要添加**spring-boot-starter-thymeleaf**
```xml
org.springframework.boot
spring-boot-starter-thymeleaf
```
## 如何使用
------
1. 配置静态资源配置**webConfig.java**
```java
//webConfig.java
@Configuration
public class webConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/", "classpath:/templates/");
}
}
```
2. 当然这些在**html**上还不能用,还需要在**html**标签上添加**xmlns:th**属性,值为**http://www.thymeleaf.org**
```html
Title
```
3. 到这里,你就可以愉快的使用**thymeleaf**
## 常用的themeleaf标签
------
| 标签 | 用途 | 例子 |
| :------------- | ------------------------------- | --------------------------------------------------- |
| [th:id]() | 替换当前标签的id属性 | `` |
| [th:if]() | 判断事件 | `show
` |
| [th:unless]() | 判断事件[相当于else] | `show
` |
| [th:each]() | 循环事件 | `| value |
` |
| [th:text]() | 替换标签内容 | `none
` |
| [th:utext]() | 替换标签内容,支持html的文本 | `none
` |
| [th:value]() | 替换标签value属性 | `` |
| [th:style]() | 设置标签样式 | `none
` |
| [th:onclick]() | 设置标签点击事件 | `none
` |
| [th:href]() | 设置标签链接 | `none` |
| [th:switch]() | 分支判断,需要与th:case配合使用 | `` |
| [th:case]() | th:switch的分支 | `none` |
| th:src | 设置img标签的图片路径 | `
` |
| th:action | 替换表单的action属性 | `