Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put static files such as CSS in a spring-boot project?

In my current spring-boot project, my views have this line:

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

to reference a static css file. When I run the project, and access one of the views which reference this file, I get a 404 not found error or a 403 unauthorized error, depending where I put the file inside the project.

I try this so far:

src/main/resources/static/css (with this, I use css/signin.css instead of signin.css)  src/main/resources/templates/static/css (with this, I use css/signin.css instead of signin.css)  src/src/main/resources/templates/acesso (same folder of the html file) 

what the right place to store this type of files?

like image 471
Kleber Mota Avatar asked Nov 27 '14 12:11

Kleber Mota


People also ask

Where should I store CSS files?

One solution is put all the css file in root-directory/css , while quite a few websites use hierarchical directory like '/skin' '/global' etc.

Where does Spring MVC put CSS files?

Put your css/js files in folder src/main/webapp/resources . Don't put them in WEB-INF or src/main/resources .

Where do you put external CSS files?

External CSS Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.


1 Answers

Anywhere beneath src/main/resources/static is an appropriate place for static content such as CSS, JavaScript, and images. The static directory is served from /. For example, src/main/resources/static/signin.css will be served from /signin.css whereas src/main/resources/static/css/signin.css will be served from /css/signin.css.

The src/main/resources/templates folder is intended for view templates that will be turned into HTML by a templating engine such as Thymeleaf, Freemarker, or Velocity, etc. You shouldn't place static content in this directory.

Also make sure you haven't used @EnableWebMvc in your application as that will disable Spring Boot's auto-configuration of Spring MVC.

like image 177
Andy Wilkinson Avatar answered Sep 19 '22 11:09

Andy Wilkinson