2

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?

4
  • 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.css Commented Jul 15, 2015 at 18:51
  • 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.html Commented Jul 16, 2015 at 10:56
  • 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. Commented Jul 16, 2015 at 21:31
  • i slove my problem when <mvc:resources mapping="/resources/**" location="/resources/" /> this code add spring-config.xml Commented Sep 4, 2015 at 11:14

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.