Serving Static Content in Spring
by, 11-27-2011 at 10:06 PM (2584 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.
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.Java Code:<?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:resources mapping="/resources/**" location="/resources/" /> </beans>