2

I have recently wanted to add some image on a page within my Spring Boot application, templated with Thymeleaf.

Even other assets (js, css) are being loaded and served successfully, images inside permitted img folder is not being served, a broken image is shown on the designated page.

This is where the image resides:

src/main/resources/static/img/image.png

Excerpt from WebSecurityConfig:

.antMatchers("/css/**", "/js/**", "/img/**","/uploads/**","**/favicon.ico").permitAll()

from the HTML (tried 2 ways, one with Thymeleaf and one without using th prefix):

<img class="featurette-image pull-left" th:src="@{../static/img/image.png}" />

<img src="../static/img/image.png"/>

Please guide me on how to proceed?

Ps: I am using Java DSL, instead xml based configuration.

I've made the search and and seen a couple solutions did not fit my situation. (Checked answers on these before:

Spring Security Thymleaf static resources don't load

when spring security enabled, resouces(css or js) can't be loaded

HTML Image not displaying, while the src url works )

1 Answers1

4

You don't need "static" in your reference to the image, because Spring Boot by default adds those paths to the classpath (source):

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

So your reference to the file should be directly:

th:src="@{/img/image.png}"
hovanessyan
  • 30,580
  • 6
  • 55
  • 83