M
M
MarkusEfr2019-05-21 23:31:43
Spring
MarkusEfr, 2019-05-21 23:31:43

How to display an image on a page in spring, thymeleaf?

Greetings.
I am writing a web application on spring boot, thymeleaf, bootstrap and a little more. I'm trying to display an image on the page, but it doesn't work. The structure of the project and how I do it can be seen on the screenshot . I
5ce45f7f06170073360596.jpeg
also tried to do as they write here https://stackoverflow.com/questions/51083414/cant-... and in other ways, but so far I have not been able to solve the problem.
5ce45fb537a8c477191566.jpeg
Anyone know how to do this please help.
5ce465a4ddfd0505118226.jpeg
Judging by the answer, the problem is in the path to the resource, but how can I set it correctly? ../ at the beginning of the path does not help, as well as writing the full path from the project to the image.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan Hasanli, 2019-05-22
@MarkusEfr

Hello!
Let's start with the simplest...
Suppose you have uploaded images to the img folder, which is in static ( resources/static/img/myImage.jpg)
If you use the thymeleaf template engine, then in the template you need to specify:
You have an error on the screen, since you simply inserted the image into the src attribute , and the paths are not correct (404 error), since the context path is not taken into account. You probably have an application opened by URL localhost:8080/contextPath
By the way, URLs to internal links, scripts and styles are set in the same way ( th:href="@{/url/url}" )
I also noticed a small bug, I don’t know what it is connected with, maybe intellij idea, maybe spring boot...
In general, even if you restart the application, even with correct file paths, it can be 404 or 500 (as far as I understand, this happens when spring security is configured). To avoid these errors, you need to make a Rebuild project, and then run it. I've only encountered this in one project.

M
maksfax, 2020-04-22
@maksfax

I had a similar situation. The picture does not open either by or by. How can I solve this problem <img th: src = "@ {/ images / polygon.png}" /><img src = "/ images / polygon.png" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question