Display image in JSP using SPRING MVC

I am trying to display an image on 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 already tried to use the image

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

and

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

Is this problem due to my hierarchy of locations where I posted my image?

I really appreciate your help. Thanks.

UPDATE :

I found a solution to my problem at: http://www.tutorialspoint.com/spring/spring_static_pages_example.htm

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

I really appreciate all your kind answers. :)

+11
java url spring-mvc jsp image
source share
7 answers

Any static resource also looks for URL mappings in spring mvc, so static resources must be defined in springmvc-servlet.xml .

Add the following MVC configuration. I assume your static files are in the resources folder.

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

then static files can be accessed from the page.

 <img src="/resources/images/logo.jpg" /> 
+14
source share

In order not to explicitly specify the path to the context, you can use the jstl core and do it like

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

You can also check this thread about spring ressource and path

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

+3
source share

to try

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

Although it is a Spring MVC application, it should still be deployed like a regular webapp. Check your deployment to make sure, and use a browser to verify downloads.

+1
source share

To do this, 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"/> 
+1
source share

I put the image folder in the WEB-INF directory, after fully configuring it in the spring -dispatcher-servlet.xml file, I used this img src: <img src = " project_name /../ images / logo.jpg " / "> on my The jsp page finally displays an image.

0
source share

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 must create a folder under web-inf called images, and in web.xml your shoul servlet mapping will be like <url-pattern>/</url-pattern> .

0
source share

TRY THIS ! ALWAYS WORKS FINE!

  1. Create img folder in src / main / resources
  2. Copy the image to this folder called "IMG"
  3. Write inside
  4. Use this picture inside

Check out the screenshots and enjoy!

enter image description here

enter image description here

0
source share

All Articles