user2817233
user2817233

Reputation: 23

CSS is not linking up and not working with Spring Boot application

enter image description here

This is the the picture of my project. I have my css file in static/css folder and I am trying to link it with my index.jsp page but it's not working. NB : Hard refresh, cache clear everything is done and I have tried more than 10 times. :( is there any other way to link up css with spring boot application?

Upvotes: 0

Views: 8812

Answers (3)

user2817233
user2817233

Reputation: 23

For me now I just put my CSS, JS, images in src/main/resources/static folder and the link is like : /css/style.css or /js/custom.js is working fine for spring boot application.

Upvotes: 2

Moshe Arad
Moshe Arad

Reputation: 3733

The solution to your problem is very simple, just add to your application.properties file (your configuration file) the next 2 lines of code:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

this will bust the cache in your browser, and when you'll make changes regarding to static content (like: css files, js files, html files), it will take place.

Spring Boot CSS Showing up blank / not loading after trying everything

Good Luck.

Upvotes: -1

Patrick
Patrick

Reputation: 12734

In the Spring-boot documentation you can read where Spring-boot reads static resources by default.

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.

If you change the location of your static files to the default one you should be able to reach them by /css/style.css

P.S. here you can find a simple good structured example.

Upvotes: 5

Related Questions