Rectangle 27 1

How to handle static content in Spring MVC?


<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>
<url-pattern>

@hamo why is that a problem? (this is a genuine question, not a argumentative retort). You will need be running the server (jboss/tomcat/jetty) anyways for spring to run, right?

Again, not sure if this is what you are after, but it worked for me. I think "default" is the name of the default servlet within JBoss. Not too sure what it is for other servers.

I actually don't want to use the default servlet- that couples me to jboss/tomcat

I had the same issue where raw output was shown with no css styles, javascripts or jquery files found.

I just added mappings to the "default" servlet. The following was added to the web.xml file:

If I understand your issue correctly, I think I have found a solution to your problem:

This should filter out the javascript and css file requests from the DispatcherRequest object.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


This problem is solved in spring 3.0.4.RELEASE where you can use <mvc:resources mapping="..." location="..."/> configuration element in your spring dispatcher configuration file.

Though not actually "wrong", this answer is too brief because Spring's own documentation (which you referenced as your answer) appears to be missing something. Check Joris's answer for a more complete answer... not the fact that it's lengthy but the fact that he mentions the use of <c:url ...> which neither your answer nor Spring's dox make mention of -- and which proved to be a critical part of the solution.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


This problem is solved in spring 3.0.4.RELEASE where you can use <mvc:resources mapping="..." location="..."/> configuration element in your spring dispatcher configuration file.

Though not actually "wrong", this answer is too brief because Spring's own documentation (which you referenced as your answer) appears to be missing something. Check Joris's answer for a more complete answer... not the fact that it's lengthy but the fact that he mentions the use of <c:url ...> which neither your answer nor Spring's dox make mention of -- and which proved to be a critical part of the solution.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Page with image</h1>
<!-- use c:url to get the correct absolute path -->
<img src="<c:url value="/resources/img/image.jpg" />" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information -->
 <context:component-scan base-package="springmvc.web" />

    <!-- the mvc resources tag does the magic -->
 <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- also add the following beans to get rid of some exceptions -->
 <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>

    <!-- JSTL resolver -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
package springmvc.web;

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

@Controller
public class HelloWorldController {

 @RequestMapping(value="/")
 public String index() {
  return "index";
 }
}
src/
 springmvc/
  web/
   MyController.java
WebContent/
  resources/
   img/
    image.jpg
  WEB-INF/
    jsp/
      index.jsp
    web.xml
    springmvc-servlet.xml

As @Bane pointed out, the <c:url value=... /> is a key part of this solution. Would you (or anyone) mind telling me why? Thanks!

In order to use this, use a directory structure that looks like the following:

Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/> (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root.

The contents of the files should look like:

This definitely did the trick for me -- merely rozky's advice is what I started out doing but for some reason any page, other than my home-page, was rendering the resource-URL's relative to the page -- following this advice renders them relative to my app's context-root instead -- and works perfectly! Thanks!

This example should be in the Spring user guide - it's the best I've seen on this topic. Thanks Joris!

You can also use <mvc:resources mapping="/**" location="/resources/" /> and it will be mapped to the root. (i.e: The root will contain both resources AND the jsps). This can save you using c:url everywhere

thanks! I was pulling my hair out until I changed the line: <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" /> to <mvc:resources mapping="/resources/**" location="/resources/" />

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

Great! Out of all the solutions I've been searching, this worked perfectly :)

In brief, it says add a servlet mapping like this:

It doesn't seem to be Tomcat specific, is simple, and works great. I've tried a couple of the solutions in this post with spring mvc 3.1 but then had problems getting my dynamic content served.

There's another stack overflow post that has an excellent solution.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

Great! Out of all the solutions I've been searching, this worked perfectly :)

In brief, it says add a servlet mapping like this:

It doesn't seem to be Tomcat specific, is simple, and works great. I've tried a couple of the solutions in this post with spring mvc 3.1 but then had problems getting my dynamic content served.

There's another stack overflow post that has an excellent solution.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Page with image</h1>
<!-- use c:url to get the correct absolute path -->
<img src="<c:url value="/resources/img/image.jpg" />" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information -->
 <context:component-scan base-package="springmvc.web" />

    <!-- the mvc resources tag does the magic -->
 <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- also add the following beans to get rid of some exceptions -->
 <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>

    <!-- JSTL resolver -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
package springmvc.web;

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

@Controller
public class HelloWorldController {

 @RequestMapping(value="/")
 public String index() {
  return "index";
 }
}
src/
 springmvc/
  web/
   MyController.java
WebContent/
  resources/
   img/
    image.jpg
  WEB-INF/
    jsp/
      index.jsp
    web.xml
    springmvc-servlet.xml

As @Bane pointed out, the <c:url value=... /> is a key part of this solution. Would you (or anyone) mind telling me why? Thanks!

In order to use this, use a directory structure that looks like the following:

Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/> (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root.

The contents of the files should look like:

This definitely did the trick for me -- merely rozky's advice is what I started out doing but for some reason any page, other than my home-page, was rendering the resource-URL's relative to the page -- following this advice renders them relative to my app's context-root instead -- and works perfectly! Thanks!

This example should be in the Spring user guide - it's the best I've seen on this topic. Thanks Joris!

You can also use <mvc:resources mapping="/**" location="/resources/" /> and it will be mapped to the root. (i.e: The root will contain both resources AND the jsps). This can save you using c:url everywhere

thanks! I was pulling my hair out until I changed the line: <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" /> to <mvc:resources mapping="/resources/**" location="/resources/" />

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<mvc:default-servlet-handler/>

Be sure to tackle with the order of the handler when you have some other view resolvers also.

In Spring 3.0.x add the following to your servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation. You need to add the mvc namespace as well but just google for that if you don't know how! ;)

when I add this line I get: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 31 in XML document from class path resource [META-INF/spring/application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 35; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:default-servlet-handler'.

Note
Rectangle 27 1

How to handle static content in Spring MVC?


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Page with image</h1>
<!-- use c:url to get the correct absolute path -->
<img src="<c:url value="/resources/img/image.jpg" />" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- not strictly necessary for this example, but still useful, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller for more information -->
 <context:component-scan base-package="springmvc.web" />

    <!-- the mvc resources tag does the magic -->
 <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- also add the following beans to get rid of some exceptions -->
 <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>

    <!-- JSTL resolver -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
package springmvc.web;

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

@Controller
public class HelloWorldController {

 @RequestMapping(value="/")
 public String index() {
  return "index";
 }
}
src/
 springmvc/
  web/
   MyController.java
WebContent/
  resources/
   img/
    image.jpg
  WEB-INF/
    jsp/
      index.jsp
    web.xml
    springmvc-servlet.xml

As @Bane pointed out, the <c:url value=... /> is a key part of this solution. Would you (or anyone) mind telling me why? Thanks!

In order to use this, use a directory structure that looks like the following:

Since I spent a lot of time on this issue, I thought I'd share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/> (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site's root.

The contents of the files should look like:

This definitely did the trick for me -- merely rozky's advice is what I started out doing but for some reason any page, other than my home-page, was rendering the resource-URL's relative to the page -- following this advice renders them relative to my app's context-root instead -- and works perfectly! Thanks!

This example should be in the Spring user guide - it's the best I've seen on this topic. Thanks Joris!

You can also use <mvc:resources mapping="/**" location="/resources/" /> and it will be mapped to the root. (i.e: The root will contain both resources AND the jsps). This can save you using c:url everywhere

thanks! I was pulling my hair out until I changed the line: <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" /> to <mvc:resources mapping="/resources/**" location="/resources/" />

Note
Rectangle 27 1

How to handle static content in Spring MVC?


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

By doing this way, you are directing the dispatcher servlet to look into the directory resources to look for the static content.

From Spring 3, all the resources needs to mapped in a different way. You need to use the tag to specify the location of the resources.

Note