🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
引入 Thymeleaf 模板的步骤如下: **1. 引入thymeleaf依赖** ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 引入热重载组件,对于静态资源的访问尤为重要 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> ``` **2. thymeleaf模板配置** (1)可以在 ThymeleafProperties 类中找到该模板的默认配置。 ```java ------------ThymeleafProperties部分源码------------ @ConfigurationProperties( // 说明当你在application.properties或application.yml对Thymeleaf进行自己的配置时 // 应该以spring.thymeleaf为前缀的配置 prefix = "spring.thymeleaf" ) public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/templates/"; // 说明thymeleaf模板文件默认放在resources/templates/目录 public static final String DEFAULT_SUFFIX = ".html"; // 说明模板文件的后缀为 .html private boolean checkTemplate = true; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML"; private Charset encoding; private boolean cache; ``` (2)`resources/application.yml` ```yml spring: thymeleaf: cache: false #关闭页面缓存,否则开发时看不到页面变化 ```` (3)Thymeleaf 模板文件位置:`resources/templates/index.html` ```html <!DOCTYPE html> <!-- xmlns:th引入thymeleaf模板,可以用于IDEA的语法提示,不引入也可以用,不过无语法提示 --> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>欢迎使用Thymeleaf</h1> </body> </html> ``` **3. controller层** ```java @Controller public class IndexController { @RequestMapping(value = "/index") public String index() { //会自动找到templates/index.html文件 return "index"; } } ``` **4. 测试** 启动项目后访问 http://localhost:8080/index ,界面将看到如下信息。 ```html 欢迎使用Thymeleaf ``` **** Thymeleaf官网:https://www.thymeleaf.org/index.html Thymeleaf语法文档地址[10 Attribute Precedence](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#attribute-precedence) Thymeleaf表达式文档地址:[4 Standard Expression Syntax](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax)