Phuu792
Phuu792

Reputation: 181

Display image in JSP with SPRING MVC

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

Upvotes: 12

Views: 101616

Answers (7)

Daniel Movemann
Daniel Movemann

Reputation: 121

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

Upvotes: 1

Nefise &#214;zşahin
Nefise &#214;zşahin

Reputation: 89

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

Upvotes: 0

Laurent Duverg&#233;
Laurent Duverg&#233;

Reputation: 559

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

Upvotes: 1

Kyle
Kyle

Reputation: 81

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.

Upvotes: 0

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

Upvotes: 5

erencan
erencan

Reputation: 3763

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

Upvotes: 15

Scary Wombat
Scary Wombat

Reputation: 44854

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.

Upvotes: 1

Related Questions