PRAFUL
PRAFUL

Reputation: 25

I cant load image in my spring mvc project

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

Upvotes: 2

Views: 1375

Answers (2)

Ralph
Ralph

Reputation: 120771

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/" />

Upvotes: 1

Thomas Junk
Thomas Junk

Reputation: 5676

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;">

Upvotes: 1

Related Questions