Web模板引擎包括JSP、FreeMaker和Thymeleaf。官方推薦Thymeleaf。我們在這裏只介紹Thymeleaf的入門。
第1步:POM文件引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第2步:配置application.properties。
#是否開啓緩存,開發時可設置爲false,默認爲true
spring.thymeleaf.cache=true
#是否檢查模板是否存在,默認爲true
spring.thymeleaf.check-template=true
#是否檢查模板位置是否存在,默認爲true
spring.thymeleaf.check-template-location=true
#模板文件編碼
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件後綴
spring.thymeleaf.suffix=.html
第3步:書寫html文件。
<html lang="en" xmlns:th="http://www.thymeleaf.org">
最後,瀏覽器中輸入http://localhost:8080/list驗證如下:
具體參考代碼:https://github.com/alifeidao/springboot-simple chapter3