Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stylesheets in new Spring MVC 4

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 pictureenter image description here

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?

like image 269
Jack Daniel Avatar asked Feb 17 '14 08:02

Jack Daniel


3 Answers

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.

like image 200
Dave Syer Avatar answered Nov 09 '22 04:11

Dave Syer


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.

like image 21
Tom Verelst Avatar answered Nov 09 '22 04:11

Tom Verelst


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:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

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" />
like image 3
paxrex Avatar answered Nov 09 '22 03:11

paxrex