Phuu792 Phuu792 - 2 years ago 2197
Java Question

How to display image in JSP with SPRING MVC

I am new to spring mvc and I am trying to display an image on JSP.
My image file is located at


And my JSP pages are located at


I have already tried to use

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

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

Is it because my webapp hierarchy is not like
MyApp\src\main\webapp\web-inf\views\home.jsp ???

Really appreciate your help.
Thank you.

UPDATE: I've found the solution to my problem.

I just have to use resource mapping in my servlet.xml.
I really appreciate all of your kind answers. :)

Answer Source

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" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download