14

I am doing a project with spring MVC and Thymeleaf. I have a question about how I should reference my CSS files if I have this folder structure:

src
  main
    webapp
     resources
       myCssFolder
         myCssFile.css
     web-inf
       spring
       views
         myViewFolder
           index.html

My configuration class is like this:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
    registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
    registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
    registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
    registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
}

And I call href in my index file like this:

href="resources/css/bootstrap.min.css"

But there are some elements that are kind of messed up in my page, for example the CSS is not working.

2
  • It would be good if you showed the structure of your project as well as the actual HTTP request that is being made to your server for the resource Commented Sep 24, 2014 at 10:22
  • Did you find the solution? Commented Feb 16, 2017 at 0:58

3 Answers 3

18

You will need to use th:href attribute for referring css files. Here is a sample from thymeleaf tutorial. If thymeleaf can not evaluate th:href value, it defaults to href value.

<head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all"  
      href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
 </head>
Sign up to request clarification or add additional context in comments.

3 Comments

no what @Narresh showed you was an example of how to use th:href
Your configuration seems to be correct. however, you should have your css files in webapp/css/ folder and you should refer to it using th:href="@{/css/bootstrap.min.css}"
Perhaps there was a time when this was necessary but I encounter no problems when using the standard href to load a stylesheet into an html file that uses Thymeleaf attributes. Unless parameters are being passed to the linked uri, I see no reason to use th:href.
8

I used below and worked ok enter image description here

here i used from css folder path .. not included static folder

<link rel="stylesheet" type="text/css" media="all" href="/css/index.css" th:href="@{/css/index.css}" />

Comments

6

I have such problem!That steps helped me.

  1. I have the directory /resources/css/myCSS.css. So I had put css into root like /css/myCSS.css and removed directory /resources
  2. I link MyCSS like this:

<link th:href="@{/css/MyCSS.css}" href="/css/MyCSS.css" rel="stylesheet" type="text/css" />

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.