Thursday, June 30, 2011

Response Header with MVC interceptors in Spring

Response Header's in Spring can be easily set using org.springframework.web.servlet.support.WebContentGenerator as this is a abstract class so we use a direct known sub class for the same which is org.springframework.web.servlet.mvc.WebContentInterceptor , this Interceptor checks and prepares request and response. Checks for supported methods and a required session, and applies the specified number of cache seconds.

Here is the Example, this should be under beans tag in your server-config.xml file

<mvc:annotation-driven />
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="120"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="requireSession" value="false"/>
<property name="useCacheControlNoStore" value="true" />
<property name="cacheMappings">
<props>
<prop key="/**/*.html">2000</prop>
<prop key="/**/*.css">500000</prop>
<prop key="/**/*.js">2592000</prop>
</props>
</property>
</bean>
</mvc:interceptors>

In CacheMapping attribute we can specify the cache time for different file types, as this increases the preformance of application.

No comments:

About Me