问题
The image is in the following directory:
/src/main/resources/static/images/logo.png
And this is my HTML tag:
<img width="220px" height="70px" th:src="@{/static/images/logo.png}"/>
However, when i'm trying to access the image, this error shows up:
There was an unexpected error (type=Not Found, status=404). No message available
回答1:
You don't need static
in the path. You should use @{/images/logo.png}
, because default resolver maps /src/main/resources/static/
to /
url in your case.
From documentation
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.
回答2:
Also make sure that your image file name doesn't have any dash on it. For example if there is an image named 'book-icon.png' the following will fail to compile:
<img width="220px" height="70px" th:src="@{/static/images/book-icon.png}"/>
Rename your file to 'bookicon.png' (without dash) instead. This code will work:
<img width="220px" height="70px" th:src="@{/static/images/bookicon.png}"/>
来源:https://stackoverflow.com/questions/46877728/image-does-not-show-using-thymeleaf-and-spring