1

I'm building a Spring MVC application with Thymeleaf as the template language. I tried so many things but I was not able to include my CSS file in my Thymeleaf templates. What am I doing wrong?

My project structure is as follows.

project
    src/
        main/
            java/
            resources/
    web/
        WEB-INF/
            web.xml
            dispatcher-servlet.xml
            views/
                css/
                    bootstrap.min.css (and other css files)
                index.html

And my index.html looks as follows.

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>
    <title>Hello Spring MVC</title>
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
</head>
<body>
...
</body>
</html>

When I run my application, the index.html is shown, but the css is not found.

How do I need to include the css?

1
  • Your code looks absolutely fine. Problem is somewhere else. Do you have Spring Security configuration by any chance? E.g. Spring Sec. will block access to your CSS if you do not allow it explicitly. Or how about to show your configuration classes/XML's generally. Commented Mar 24, 2017 at 17:01

2 Answers 2

4

Did you try to remove the leading "/"? Like this:

<link th:href="@{css/bootstrap.min.css}" rel="stylesheet"/>

Did that work?

Also, many IDE's will allow you to drag and drop a file which will create a link to that file. In Netbeans for example, you can drag a file from the project navigator onto an HTML (or HTML-like) open file and it will create a link.

Try:

project
src/
    main/
        java/
        resources/
            static/ 
                bootstrap.css 
Sign up to request clarification or add additional context in comments.

4 Comments

I just tried, it doesn't work. I also tried using drag/drop, but it creates the same link as above (css/bootstrap.min.css).
I've looked at an older Spring boot repo of mine and I put the CSS under resources / static. In the main directory. So it's in src / main / resources / static / my.css See if your templates can find it there.
Do you have any mapping for the folders?
I did not specifically map anything. It was an older version of Boot and at the time it just looked in the resources folder for web-facing files --it might be different now.
1

Did you configure your resource handler correctly? I. e.

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    // ...
    registry.addResourceHandler("/css/**")
        .addResourceLocations("classpath:/views/css/");
    // ...
}

XML configuration example can be seen here

1 Comment

Agreed, this should be the right answer. Has to found as resource, has to have the right the right link syntax, and has to be permitted.

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.