首先 在Spring Boot項目中使用Thymeleaf模板,首先必須保證引入Thymeleaf依賴,示例代碼如下:
```html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
其次,在全局配置文件中配置Thymeleaf模板的一些參數。一般Web項目都會使用下列配置,示例代碼如:
```properties
spring.thymeleaf.cache = true #啟用模板緩存
spring.thymeleaf.encoding = UTF-8 #模板編碼
spring.thymeleaf.mode = HTML5 #應用於模板的模板模式
spring.thymeleaf.prefix = classpath:/templates/ #指定模板頁面存放路徑
spring.thymeleaf.suffix = .html #指定模板頁面名稱的后綴
```
上述配置中,spring.thymeleaf.cache表示是否開啟Thymeleaf模板緩存,默認為true,在開發過程中通常會關閉緩存,保證項目調試過程中數據能夠及時響應;spring.thymeleaf.prefix指定了Thymeleaf模板頁面的存放路徑,默認為classpath:/templates/;spring.thymeleaf.suffix指定了Thymeleaf模板頁面的名稱后綴,默認為.html
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
學習讓人快樂,學習更讓人覺得無知!學了1個多月的《Java工程師高薪訓練營》,才發現自己對每個技術點的認知都很膚淺,根本深不下去,立個Flag:每天堅持學習一小時,一周回答網上3個技術問題,把自己知道都分享出來。