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?

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@RaviKhakhkhar Can you accept this answer if it worked for you?
@Drahakar : I haven't asked this question. So naturally I don't have right to accept this answer. I have already up-voted.
this step does not work for me. I moved image folder outside WEB-INF as stated by Nada Lu
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
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.

Comments

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.

Comments

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"

Comments

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.

3 Comments

That didn't work. Even trying to directly access the image (localhost:8080/myapp/Images/face.png) didn't work. Do I need to configure spring mvc to enable access to images?
See this question: stackoverflow.com/questions/553749/…. Might help.
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?
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 :)

Comments

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:

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.