Serving Javascript Resources

Spring JS provides a generic ResourceServlet to serve web resources such as JavaScript and CSS files from jar files, as well as the webapp root directory. This servlet provides a convenient way to serve Spring.js files to your pages. To deploy this servlet, declare the following in web.xml:

<!-- Serves static resource content from .jar files such as spring-js.jar -->
<servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
</servlet>
        
<!-- Map all /resources requests to the Resource Servlet for handling -->
<servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>
        

Note that starting with version 3.0.4, the Spring Framework includes a replacement for the ResourceServlet (see the Spring Framework documentation). With the new <mvc:resources> element resource requests (.js, .css) are handled by the DispatcherSevlet without the need for a separate ResourceServlet. Here is the relevant portion of the Spring MVC configuration in the mvc-booking sample:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<mvc:annotation-driven/>

	<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />

	...
	
</beans>	

        

This incoming maps requests for /resources to resources found under /META-INF/web-resources on the classpath. That's where Spring JavaScript resources are bundled. However, you can modify the location attribute in the above configuration in order to serve resources from any classpath or web application relative location.

Note that the full resource URL depends on how your DispatcherServlet is mapped. In the mvc-booking sample we've chosen to map it with the default servlet mapping '/':

<servlet>
	<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

        

That means the full URL to load Spring.js is /myapp/resources/spring/Spring.js. If your DispatcherServlet was instead mapped to /main/* then the full URL would be /myapp/main/resources/spring/Spring.js.

When using of the default servlet mapping it is also recommended to add this to your Spring MVC configuration, which ensures that any resource requests not handled by your Spring MVC mappings will be delegated back to the Servlet container.

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	...

	<mvc:default-servlet-handler />
	
</beans>