0

I added a simple page containing single image which is in '/resources/images/' folder but when run on server, images don't load on the page. Followed many blogs but unable to resolve this problem. The controller mapping is as follows:

@RequestMapping(value = "/image", method = RequestMethod.GET)
public ModelAndView imagepage() {
    ModelAndView img = new ModelAndView("image_check");
    return img;
}

image_check.jsp file is as follows:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<img src="/resources/ABC_bank.png" alt="Smiley face" height="42" width="42">
</html>

project-servlet.xml is as follows:

<bean id="viewResolver" 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/images/" />

images are present in '/webapp/resources/images/' folder. The page opens but the images don't load.

Raviteja Reddy
  • 109
  • 2
  • 14

2 Answers2

0

Make the following change in project-servlet.xml.

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

Then refer the image like

<img src="/resources/images/logo.jpg" />

Check this as reference.

  • Check your folder structure. The resource folder should be under web folder. Folder structure : web-> resources->image – Md khirul ashik Oct 12 '17 at 01:16
  • The resource folder is under `/webapp` folder. The path for the image is `/webapp/resources/images/ABC_bank.png`. The answer provided by me worked quite well. And it is the actual process to access the static references. – Raviteja Reddy Oct 12 '17 at 04:27
0

The static references can be accessed in jsp file by adding the following tag

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

The following line maps /resources/ to /resources/images/ folders:

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

So the final modified code for the image_check.jsp is as follow:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<img src="<c:url value="/resources/ABC_bank.png"/>">
</html>
Raviteja Reddy
  • 109
  • 2
  • 14