2
votes

In my Spring Boot project, when I do a maven build, my css stylesheet is not located. I have already taken advice from the official Spring Boot documentation and other stackoverflow posts, and have put my css file in resources/static/css directory.

enter image description here

|_src
   |_main
   |_java
   |_resources
      |_templates
      |_static
         |_css
            |_master.css

Declaring the css stylesheet in the thymeleaf layout (html file under templates directory):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ipp="" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" href="../static/css/master.css" th:href="@{/css/master.css}" media="screen"/>
</head>

My pom.xml includes spring-boot-starter-web and spring-boot-starter-thymeleaf, so it is expected that the Spring-Boot ResourceLocations should map static content to src/main/resources/static.

I have also attempted:

th:href="@{css/master.css}"

AND

th:href="@{static/css/master.css}"

AND

th:href="@{../static/css/master.css}"

AND complete removal of th:href=...

Any ideas on what is preventing the stylesheet from being located?

1
This is the correct version th:href="@{css/master.css}" show me the rendered HTML output. Also try to access the css directly using localhost:8080/css/master.cssFaraj Farook
The templates and static should be stored under /WEB-INF instead of /resources. This resource could be helpful I think: thymeleaf.org/doc/tutorials/2.1/thymeleafspring.htmlcodingtim
I had tried storing the templates and statics under the different options that Spring Boot maps to including /WEB-INF. After 5 days, it magically started working under the current directory structure, but I am unaware of what the issue was initially? It could not have been a modification in code because the issue spanned over multiple separate projects, and then started working for all of them.Amy Slawson
i slove my problem when <mvc:resources mapping="/resources/**" location="/resources/" /> this code add spring-config.xmlKisspa

1 Answers

0
votes

I see that you have a correct project tree. This means that all css, js, image files must be in the static folder which also needs to be in the following folder src/main/resources/

Folder static is served from /. Something like that, src/main/resources/static/master.css will be served from /master.css

More explanations

package com.lab;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 *
 * @author mdbah
 */

@EnableWebMvc 
@Configuration
public class Webconfig extends WebMvcConfigurerAdapter{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}

I also used this class to include bootstrap by webjars in my project, which prevented loading my css and js files from the static folder. Because of this, I simply removed the @EnableWebMvc annotation, then recompiled the project. And now, voila

If you are in this context, this method regulates your concern

PS: Sorry, i'm not speak english well, hope you will understand something