View RSS Feed

Spring Framework

Serving Static Content in Spring

Rate this Entry
by , 11-27-2011 at 11:06 PM (2820 Views)
If you are concern you that DispatcherServlet will be handling static content requests, Spring has features that can help you to deal with this. Spring’s mvc namespace includes a <mvc:resources> element that handles requests for static content for you. In the following example, I show you how you can create an xml file that the DispatcherServlet can use to create an application context.

Java Code:
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns=""
   <mvc:resources mapping="/resources/**" location="/resources/" />
Since all requests will go through DispatcherServlet to be handled in some way, in most cases it will be via controllers. But requests for static content also need to be handled by DispatcherServlet. <mvc:resources> configures a handler for serving static content. Just set the mapping attribute to /resources/**, including an Ant-style wildcard as shown above to indicate that the path must begin with /resources, but may include any subpath.

Submit "Serving Static Content in Spring" to Facebook Submit "Serving Static Content in Spring" to Digg Submit "Serving Static Content in Spring" to Submit "Serving Static Content in Spring" to StumbleUpon Submit "Serving Static Content in Spring" to Google

Updated 11-30-2011 at 01:59 PM by Spring Framework