Mesut Dogan
Mesut Dogan

Reputation: 592

Spring boot resource static folder

Shortly I have application which is structered as below and the problem is that spring boot not scanning static folder recursively. I mean if i put bootstrap.min.css to static/css/bootstrap.min.css it is okay no problem but if i put into another folder in subpackage of static folder static/bootstrap/bootstrap.min.css , just like in the screenshot then I am not able load these resources. How can I handle this ? Thanks in advance. enter image description here

Upvotes: 0

Views: 626

Answers (1)

Marc Tarin
Marc Tarin

Reputation: 3169

Your URL lacks a leading slash.

http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls

There are different types of URLs:

  • Absolute URLs: http://www.thymeleaf.org
  • Relative URLs, which can be:
    • Page-relative: user/login.html
    • Context-relative: /itemdetails?id=3 (context name in server will be added automatically)
    • Server-relative: ~/billing/processInvoice (allows calling URLs in another context (= application) in the same server.
    • Protocol-relative URLs: //code.jquery.com/jquery-2.0.3.min.js

The URL in your screenshot, bootstrap/css/bootstrap.min.css, is page-relative.

What you want is a context-relative URL. When your file is in src/main/resources/static/css/bootstrap.min.css, use:

th:href="@{/css/bootstrap.min.css}"

When it's in in src/main/resources/static/bootstrap/bootstrap.min.css, use:

th:href="@{/bootstrap/bootstrap.min.css}"

Furthermore, instead of manually downloading bootstrap.min.css, I suggest you use the Bootstrap WebJar and then use the following href:

th:href="@{/webjars/bootstrap/3.3.6/css/bootstrap.min.css}"

Upvotes: 2

Related Questions