Image does not show using thymeleaf and spring

二次信任 提交于 2021-02-08 06:50:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!