I started a new web project in Spring MVC 4(Spring Boot) and my question is where I must put my css files? I'm using thymeleaf template engine and my folder structure as in picture
as can u see, I trying create a CSS folder under the resources folder, but the link <link rel="stylesheet" href="/resources/css/main.css" />
from index.html is not working. any ideas?
Per the howto docs:
Spring Boot by default will serve static content from a folder called /static (or /public or or /resources or /META-INF/resources) in the classpath or from the root of the ServeltContext.
There's also a sample or two, e.g. this one.
Make sure you are packaging your project as a war
in your pom.xml
.
The /src/main/resources
folder is usually deployed to WEB-INF/classes
, so it won't be accessible directly from the context.
However, The /src/main/webapp
is normally deployed to /
(root of your web app), which is accessible from the context.
You should put your web resources under /src/main/webapp
(e.g. /src/main/webapp/css
). These are then automatically deployed under the context root of your web application. They are then accessible from e.g. /css
.
<link rel="stylesheet" th:href="@{/css/main.css}" />
I'd also move your templates to /src/main/webapp/WEB-INF/templates
.
If you read the SpringBoot documentation you will see that Spring Boot will automatically add static web resources located within any of the following directories:
You have created folder named "css" and you put your file "main.css" inside this folder. So you should use relative path when referencing this file inside your HTML
<link rel="stylesheet" href="/css/main.css" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With