2

I am using spring tool suite. I have placed the image file in folder WebContent/WEB-INF/resources

<img src="WebContent/WEB-INF/resources/team_pic1.jpg" alt="Mountain View" style="width:304px;height:228px;">

This is my servlet.xml code

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

I am getting error as

Failed to load resource: the server responded with a status of 404 (Not Found) at http://localhost:8080/SpringMVCTest/WebContent/WEB-INF/resources/team_pic1.jpg

Here is my directory structure

enter image description here

PLS let me know where I am going wrong

here is the error I get when I run through browser

s

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
PRAFUL
  • 25
  • 6

2 Answers2

1

Just try:

<c:url var="imgUrl" value="/resources/team_pic1.jpg" />
<img src="${imgUrl}" alt="Mountain View" style="width:304px;height:228px;">

this build the right URL to hit the resource handler.


You also have to correct the resource mapping:

  • if you want to deliver "subresources" then you need to add /**.
  • Depending on where your resource files are located, you also need to correct the location attribute: I expect that /WEB-INF/resources/ is the folder where your resources exist in the war!

So I think this it the resource configuration you need:

<resources mapping="/resources/**" location="/WEB-INF/resources/" />
Ralph
  • 118,862
  • 56
  • 287
  • 383
1

Content is (usually) served from WEB-INF, so it is not part of the path.

Your configuration defines the mapping

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

which says nothing more, that static resources are served from /resources, which means, they are delivered as is.

You could use ${pageContext.request.contextPath}- explanation here

<img src="${pageContext.request.contextPath}/resources/team_pic1.jpg" alt="Mountain View" style="width:304px;height:228px;">
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43