[英]Spring Boot, Thymeleaf and CSS
這真的很愚蠢,但我無法讓它工作。
在我的 spring 啟動 mvc 應用程序中,我假設 5 個百里香葉模板,其中一個是 error.html。
當請求任何無效路由時,error.html 會派上用場。
當無效路由嵌套(如 2 級或更多級別)然后 css 不適用時,會發生此問題。
例如:
http://localhost:3000/application/index- valid route and css is applied
http://localhost:3000/application/success- valid route and css is applied
http://localhost:3000/application/failure- valid route and css is applied
http://localhost:3000/application/invalidroute- route does not exist but css is applied
http://localhost:3000/application/invalidroute/something - route does not exist and css is also not applied
我的 CSS 位於 static 文件夾下的 css 文件夾中
所有 thymeleaf 模板都在同一級別,並通過以下方式訪問 css
<link rel="stylesheet" href="css/main.css"/>
在控制台中看到錯誤
GET http://localhost:3000/application/invalidroute/css/main.css net::ERR_ABORTED 404
代替
<link rel="stylesheet" href="css/main.css"/>
用這個:
<link rel="stylesheet" th:href="@{/css/main.css}" />
並確保這是您的文件占位符
src/main/resource/static/css
- 用於 CSS 文件
src/main/resource/templates
- 用於 HTML 模板文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.