12

I am trying to display an image on a jsp. My image file is located at

MyApp/WebContent/images/logo.jpg

And my JSP pages are located at

MyApp/WebContent/WEB-INF/view/home.jsp

I have already tried to use the image by

<'img src="<%=request.getContextPath()%>/images/logo.jpg" />

and

<'img src="<'c:url value='<%=request.getContextPath()%>/images/logo.jpg'></c:url></img>

Is this issue something because of my location hierarchy where I have placed my image?

Really appreciate your help. Thank you.

UPDATE:

I've found the solution to my problem in: http://www.tutorialspoint.com/spring/spring_static_pages_example.htm

I just have to use resource mapping in my servlet.xml.

I really appreciate all of your kind answers. :)

Mike
  • 721
  • 1
  • 17
  • 44
Phuu792
  • 181
  • 1
  • 1
  • 9
  • [![Process to add image](https://i.stack.imgur.com/1D2z6.png)](https://i.stack.imgur.com/1D2z6.png)Please follow the steps in this picture.. :) .[! – Ashish Pandey May 19 '17 at 16:20

7 Answers7

15

Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml.

Add the following entry to your MVC configuration. I assume that your static files in resources folder.

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

then static files can be accessible from the page.

<img src="/resources/images/logo.jpg" />
erencan
  • 3,725
  • 5
  • 32
  • 50
  • 1
    add image in MyApp/src/main/webapp/WEB-INF/images/logo.jpg and then add the mvc resource mapping in finally add the following in the jsp – Gopal Jul 21 '15 at 02:39
5

To avoid to have to indicate explicitly the context path you can use jstl core and do it like that

<img src="<c:url value="/images/logo.jpg"/>"/>

You can also check this thread about spring ressource and path

Spring 3 MVC resources and tag <mvc:resources />

Community
  • 1
  • 1
1

try

<img src="/MyApp/WebContent/images/logo.jpg" />

Even though it is a Spring MVC app, it should still deploy as a normal webapp. Check your deployment to make sure, and also use the browser to test loading.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Yes, the problem is that I can see the image in JSP viewer in Eclipse but when I deploy and go to the image url, i get HTTP 404 and the image itself on JSP page is broken. Any idea? T___T – Phuu792 Dec 04 '13 at 07:43
  • I did not say anything about Eclipse. Check you webserver's webapp directory to make sure it is deployed correctly, or at least find out where it is. Verify by typing the url into you browser's address bar – Scary Wombat Dec 04 '13 at 07:47
  • I have put my image folder under "MyApp" (MyApp/images/logo.jpg) and http://localhost:8080/MyApp/images/logo.jpg is HTTP 404 not found. My spring url pattern to jsp page is "http://localhost:8080/MyApp/login.htm". Is there a way to make the folder accessible by url in spring? I am so sorry for being so clueless. – Phuu792 Dec 04 '13 at 08:02
  • so below the tomcat installation is there a file webapps/MyApp/images/logo.jpg? If so it should be accessible. – Scary Wombat Dec 04 '13 at 08:12
  • No, I run the application by "<'Context path="/MyApp" reloadable="true" docBase="C:\KKMT\eclipse\workspace\MyApp\WebContent" workDir="C:\KKMT\eclipse\workspace\MyApp\WebContent\WEB-INF\classes" />" in "server.xml" in tomcat. :( I can send you all screen shot and files. I can't add images here T_T My email is "blingbling792@gmail.com" Really appreciate your help as it is the last thing I need to add for my project. – Phuu792 Dec 04 '13 at 08:35
  • So your workspace is also being used as you webapps dir? Not the best of practices. I can see that this would cause a non-proper deployment to be done. Try using the tomcat webapps dir and then do a Eclipse `deploy to server` – Scary Wombat Dec 04 '13 at 08:48
1

To make it work I had to do in spring config:

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

In JSP:

<spring:url value="/resources/images" var="images" />
    <img src="${images}/back.png"/>
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
1

TRY THIS ! ALWAYS WORKS FINE !

  1. Create your img folder at src/main/resources
  2. Copy the picture inside this folder called "img"
  3. Write inside
  4. Use this picture inside

check the screenshots and enjoy !

enter image description here

enter image description here

Daniel Movemann
  • 101
  • 1
  • 2
0

I put images folder under WEB-INF directory, after did fully configuration in the spring-dispatcher-servlet.xml file, I used this img src:< img src="projectname/../images/logo.jpg" /> in my jsp page, images display finally.

Kyle
  • 81
  • 1
  • 3
0

in springmvc-servlet.xml you should add <mvc:resources location="/WEB-INF/images/" mapping="/images/**" /> and in jsp <img src="images/logo.jpg" /> and you should create a folder under web-inf which is named images and in the web.xml your servlet mapping shoul be like that <url-pattern>/</url-pattern>.