14

I used Netbeans to create a Spring MVC 3.0 app. I have a simple controller and JSP view. The JSP view appears correctly except for an image that doesn't render. My directory structure looks like this: alt text

In my Home.jsp page, the image that doesn't render is referenced like so:

<img src="Images/face.png" />

I've verified that face.png is in the Images directory. So why doesn't it appear in the browser? In Spring MVC, where should I place files referenced by JSP views, like images, CSS, JS, etc?

Sajee
  • 4,317
  • 14
  • 46
  • 54

7 Answers7

37

Alter your web.xml file:

<servlet>
    <servlet-name>spring-servlet</servlet-name>
    <servlet-class>com.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>spring-servlet</servlet-name>
    <url-pattern>/<url-pattern>
</servlet-mapping>

and add below configuration after it:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

If you have a better solution please share with me.

bluish
  • 26,356
  • 27
  • 122
  • 180
Gofier
  • 459
  • 1
  • 5
  • 7
  • @RaviKhakhkhar Can you accept this answer if it worked for you? – Drahakar Sep 16 '12 at 20:33
  • @Drahakar : I haven't asked this question. So naturally I don't have right to accept this answer. I have already up-voted. – Ravi Khakhkhar Sep 17 '12 at 05:42
  • 1
    this step does not work for me. I moved image folder outside WEB-INF as stated by [Nada Lu](http://stackoverflow.com/questions/4169266/where-to-place-images-css-in-spring-mvc-app#) – Sumit Ramteke Jan 03 '14 at 09:04
  • I have a problem with this... It's all working fine, But when we use bootstrap all the images used by bootstrap is not loading – Jay Prakash Oct 26 '17 at 13:55
3

I was able to find a workable answer here: How to handle static content in Spring MVC?

The problem was that my spring mvc dispatcher servlet was intercepting the calls to static resources. So I mapped Tomcat's default servlet to handle the static resources.

Community
  • 1
  • 1
Sajee
  • 4,317
  • 14
  • 46
  • 54
3

anything under Web-Inf folder will be marked as private and can not be access by the browser. You suppose to move the image folder outside Web-Inf and under webapp.

Nuda Lu
  • 129
  • 2
  • 12
1

For set image in jsp file in Spring MVC framework :

You can simply set your image by following steps:

Step 1. Place images folder in resources

Step 2. write image path like : src="${pageContext.request.contextPath}/resources/images/logo.jpg"

Krishna Roy
  • 193
  • 1
  • 3
  • 20
0

I think you can put all the static data such as image, css, javascripts etc out side the WEB-INF and then call it the normal way :)

Tanuj Verma
  • 388
  • 2
  • 7
0

A bit old, but let me add my bit here. It is generally preferred to hide the .css/ .js files from direct public access by placing them into the main/java/webapp folder instead of the WEB-INF.

The reasons are explained very well in the posts below:

Community
  • 1
  • 1
smos
  • 148
  • 1
  • 6
0

I believe it can only reference images/css in the WEB-INF folder. Moving your Images folder to WEB-INF should fix your problem.

Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
  • That didn't work. Even trying to directly access the image (http://localhost:8080/myapp/Images/face.png) didn't work. Do I need to configure spring mvc to enable access to images? – Sajee Nov 12 '10 at 22:20
  • See this question: http://stackoverflow.com/questions/553749/spring-mvc-webapp-where-to-store-paths-to-common-images. Might help. – Ian Dallas Nov 12 '10 at 22:41
  • No, that confused me even more. :) What I'm trying to do should be easy w/o resorting to themes. Perhaps I have URL pattern that's blocking access to anything but controllers? – Sajee Nov 12 '10 at 22:49