Thymeleaf-基本使用

25次阅读

共计 1031 个字符,预计需要花费 3 分钟才能阅读完成。

Thymeleaf 基本使用
(1)Thymeleaf 模板基本配置

首先 在 Spring Boot 项目中使用 Thymeleaf 模板,首先必须保证引入 Thymeleaf 依赖,示例代码如下:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>
12345678910
其次,在全局配置文件中配置 Thymeleaf 模板的一些参数。一般 Web 项目都会使用下列配置,示例代码如:

spring.thymeleaf.cache = true #启用模板缓存

spring.thymeleaf.encoding = UTF-8 #模板编码

spring.thymeleaf.mode = HTML5 #应用于模板的模板模式

spring.thymeleaf.prefix =
classpath:/templates/ #指定模板页面存放路径

spring.thymeleaf.suffix = .html #指定模板页面名称的后缀

123456789101112

上述配置中,spring.thymeleaf.cache 表示是否开启 Thymeleaf 模板缓存,默认为 true,在开发过程中通常会关闭缓存,保证项目调试过程中数据能够及时响应;spring.thymeleaf.prefix 指定了 Thymeleaf 模板页面的存放路径,默认为 classpath:/templates/;spring.thymeleaf.suffix 指定了 Thymeleaf 模板页面的名称后缀,默认为.html
(2)静态资源的访问
开发 Web 应用时,难免需要使用静态资源。Spring boot 默认设置了静态资源的访问路径。
使用 Spring Initializr 方式创建的 Spring Boot 项目,默认生成了一个 resources 目录,在 resources 目录中新建 public、resources、static 三个子目录下,Spring boot 默认会挨个从 public、resources、static 里面查找静态资源
每天学习打卡让自己变得充实了起来,学习让人快乐,学习更让人觉得无知!学了 1 个多月的《Java 工程师高薪训练营》,才发现自己对每个技术点的认知都很肤浅,根本深不下去,立个 Flag:每天坚持学习一小时,一周回答网上 3 个技术问题,把自己知道都分享出来。

正文完
 0