1

I can't see my image at 404 page. I tried many ways to display image at my 404 page but I don't understand why spring MVC doesn't show the image. Thank you for helping.

enter image description here

Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30
  • Is the issue that the IDE is showing a warning or that you cannot see the image when it is rendered? IDEs can only be so intelligent. In this case it can't resolve the image. If it matters you could try a url relative from the root of the url or you could use an absolute url. In practice I wouldn't worry about it. – Deadron Apr 06 '21 at 21:30
  • When that 404 page is displayed in root path of site your image path is looking for a path higher than the root which can't exist. Inspect the image request in browser dev tools network tab . It's a 404 itself – charlietfl Apr 06 '21 at 21:32

2 Answers2

1

Add below code lines to your dispatcher servlet(YourFileNameservlet.xml). It's a xml file which as a Front Controller and forwards request to Spring MVC controllers for processing.

<mvc:resources mapping="/static/**" location="/static/" />

Use ${pageContext.request.contextPath} for keeping your links 'relative' to the application context. Add below code lines to your jsp file.

<img src="${pageContext.request.contextPath}/static/images/404.png">

or

<c:url var="imgUrl" value="/static/images/404.png" />
<img src="${imgUrl}">

You can refer this also -> I cant load image in my spring mvc project

Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30
0

In HTML, relative paths are relative to the root of the document, not relative to the current file. Not sure how spring operates or if your file tree changes once it's actually on a file server, but if the structure remains the same as pictured and your index existed at main/index.html then the relative path would be ./resources/static/404.png.

See this question for more details on absolute vs relative.

user3781737
  • 932
  • 7
  • 17