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/”
“/”:当前项目的根路径

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