Justin
Justin

Reputation: 887

Reference a .css file with thymeleaf in spring mvc using th:href

I have my index.html thymeleaf page coming up but the CSS I am referencing on the page is not being found.

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

Here is what the deployed WAR looks like in Tomcat: enter image description here

The index.html is in the templates folder.

But I am getting the following error in the Chrome console:

GET http://localhost:8080/pocApp/css/bootstrap.min.css 404 (Not Found)

If I can get the index.html page fine why can't the thymeleaf servlet find that file? I've tried sticking the entire css directory into various places in the directory structure such as in WEB-INF and within WEB-INF/classes with no luck.

Upvotes: 0

Views: 4197

Answers (1)

Justin
Justin

Reputation: 887

This site is quickly becoming my rubber duck as this is the second question of mine I've answered myself.

In my Spring 4 Java Config I have this:

public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
}

So I just had to restructure my directory and change the stylesheet URL.

New directory structure: enter image description here

And the new code snippet from my index.html

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

Upvotes: 1

Related Questions