Spring Boot, Thymeleaf and CSS

こ雲淡風輕ζ 提交于 2021-01-27 15:52:14

问题


This is really silly but I am not able to get it working.

In my spring boot mvc app, I have let's say 5 thyme leaf templates one of which is error.html.

error.html comes handy when a request is made for any invalid routes.

The issue happens when the invalid routes are nested (like 2 or more level) then the css dont apply.

Eg:

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

My CSS is located in css folder under the static folder

All the thymeleaf templates are at the same level and access the css by

 <link rel="stylesheet" href="css/main.css"/>

Error seen in console

GET http://localhost:3000/application/invalidroute/css/main.css net::ERR_ABORTED 404

回答1:


Instead of

<link rel="stylesheet" href="css/main.css"/>

Use this :

<link rel="stylesheet" th:href="@{/css/main.css}" />

and make sure this is your file placeholders

src/main/resource/static/css - for CSS files

src/main/resource/templates - for HTML templates files



来源:https://stackoverflow.com/questions/61553420/spring-boot-thymeleaf-and-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!