Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place images/CSS in spring-mvc app?

Tags:

spring-mvc

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?

like image 915
Sajee Avatar asked Nov 12 '10 21:11

Sajee


People also ask

Where do I put CSS files in Spring MVC?

1. Project Directory. A standard Maven folder structure, puts the static resources like js and css files into the webapp\resources folder.

How can set background image in spring boot in HTML?

You can add a background image to webpage irrespective of the framework you are using as it is html specific. Just ensure that the image is present on the mentioned path. The background image is in the directory src/main/resources ...

What is the purpose of RequestMapping?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.


1 Answers

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.

like image 156
Gofier Avatar answered Oct 15 '22 10:10

Gofier