Thymeleaf模板引擎是springboot中默認配置,與freemarker類似,能夠徹底取代jsp,在springboot中,它的默認路徑是src/main/resources/templates 靜態文件css, js 等文件默認路徑是src/main/resources/static,全部項目中若是沒有這個目錄須要手動加上了css
首先咱們要在pom.xml文件中添加依賴html
<!-- thymeleaf 模板引用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
引用以後咱們就來測試一下, 在pom.xml中引入依賴以後。你徹底能夠不用配置(也秉承了springboot 約定優於配置)固然你若是須要自定義一些屬性,你能夠在application.properties 中添加配置。java
測試類@Controllerspring
/** * @author pillarzhang * @date 2019-06-03 */ @Controller public class loginController { @RequestMapping("/index") public String index(){ return "index"; } }
Index,html 頁面以下緩存
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <p style="color:red">hello world</p> </body> </html>
啓動項目,輸入http://localhost:8081/index 便可看到以下頁面springboot
這就成功的集成了Thymeleaf。app
注意:前面也說了,若是你不配置任何屬性依然可使用,固然你也能夠本身設置,在配置文件中application.properties 設置相應的屬性jsp
spring.thymeleaf.prefix=classpath:/templates/ 設置thymeleaf路徑默認爲src/main/resources/templates spring.thymeleaf.suffix=.html 設置thymeleaf模板後綴 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false 是否開啓緩存默認爲true spring.thymeleaf.mode=LEGACYHTML5 設置thymeleaf嚴格校驗 spring.thymeleaf.encoding=UTF-8 設置編碼
至此,springboot集成thymeleaf 就完成了,雖然中間遇到了一些小問題,還好解決了。spring-boot
若有不當和錯誤之處,請指出,咱們一塊兒交流學習,共同進步!謝謝!學習