Springboot中通過thymeleaf的方式引入靜態資源

Springboot中通過thymeleaf的方式引入靜態資源

通過Thymeleaf引入靜態資源後,當我們對項目訪問路徑修改後,不用再到頁面中去修改對應的靜態資源路徑。

1.引入Themeleaf的pom座標
2.在html頁面中修改標籤,如下:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

3.使用themeleaf的標籤修改靜態資源路徑

<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.5.0/css/bootstrap.css}" rel="stylesheet">
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">

對應的路徑需要根據springboot中對靜態資源的訪問規則編寫

對於webjars,所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源;
對於靜態資源,/** 訪問當前項目的任何資源,都去(靜態資源的文件夾)找映射,如下:

“classpath:/META-INF/resources/”,
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
“/”:當前項目的根路徑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章