8

I know this question has been asked many times, but I'm not able to figure out what the problem is. I have the images folder under the src/main/webapp folder (this a maven web project). I have the index.jsp in the src/main/webapp/WEBINF/views folder.

I'm trying to access the images and other resources like css and js like this:

<img src="/images/left_arrow.png" alt="" />

But the images are not getting displayed.

Here is the web.xml file

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Here is the WEB-INF/mvc-dispatcher-servlet.xml file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.ravi.WebApp" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

</beans>

Here is the Controller package com.ravi.WebApp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping("/")
public String printWelcome(Model model) {
    return "index";

}

}

6 Answers 6

11

Try adding the following resources declaration to your Spring configuration:

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />    

Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.

Your configuration would then look like:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

And your resources would be referenced as follows:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />" alt="" />
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting this error after adding the resources declaration in Spring Configuration " No mapping found for HTTP request with URI [/WebApp/] in DispatcherServlet with name 'mvc-dispatcher'"
It worked. The change I made is I refered the images in the jsp using c:url tag <img src="<c:url value='/resources/images/ip_banner.jpg" />" alt="" />. Thanks for the help.
2

If Using annotation then make sure to user

<mvc:annotation-driven/>

with resources

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

else annotaion controller will not work

Comments

1

You just need to add a reference your image folder on Spring MVC configuration file

WEB-INF/spring-context.xml:

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

Comments

1

Please follow the steps in this picture.. :)

Step 1: Create a folder in webapp but not in WEB-INF

Create Resources then images then store your image. webapp/resources/images/fileName.jpg

Step 2: Now that you have created your folders

Let us map the path you created in the servlet configuration file where we deal with mapping of the paths Add this code : <mvc:resources mapping="/resources/*" location="/resources/" />

Step 3: Add the code for accessing the image resource from the location you created in step 1: <img src="/attendance/resources/images/logo.png" width="100px" height="100px">

Spring MVC access Image in JSP

1 Comment

it might be useful if you could briefly describe each step.
0

If you want to keep the static resources outside the WEB-INF folders in your web root and want the container to handle the static resource requests, you should add this to your application context file:

<mvc:default-servlet-handler />

The suggestion by @BeauGrantham to add resources mapping will also work.

Comments

0

The above suggestions worked for me as well. But if anyone else has trouble with the associated namespace, I had to add the mvc portion to mvc-dispatcher-serlvet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.