Display image in JSP with SPRING MVC

97,070

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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"/>
Share:
97,070
Phuu792
Author by

Phuu792

Updated on June 26, 2020

Comments

  • Phuu792
    Phuu792 almost 4 years

    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. :)

  • Phuu792
    Phuu792 over 10 years
    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
  • Scary Wombat
    Scary Wombat over 10 years
    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
  • Phuu792
    Phuu792 over 10 years
    I have put my image folder under "MyApp" (MyApp/images/logo.jpg) and localhost:8080/MyApp/images/logo.jpg is HTTP 404 not found. My spring url pattern to jsp page is "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.
  • Scary Wombat
    Scary Wombat over 10 years
    so below the tomcat installation is there a file webapps/MyApp/images/logo.jpg? If so it should be accessible.
  • Phuu792
    Phuu792 over 10 years
    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 "[email protected]" Really appreciate your help as it is the last thing I need to add for my project.
  • Scary Wombat
    Scary Wombat over 10 years
    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
  • Gopal
    Gopal almost 9 years
    add image in MyApp/src/main/webapp/WEB-INF/images/logo.jpg and then add the mvc resource mapping in <mvc:resources mapping="/images/**" location="/WEB-INF/images/" /> finally add the following in the jsp <img src="/SpringMVCExample/images/SupportDashboard.jpg"/>