Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right out of the box cache-control header filter?

Is there a right of the box cache-control response header filter that will enable me to set those cache headers on my static resources without me having to build my own Filter? It seems like such a common task. Is there a Spring filter ? I am currently using Tomcat 6.0 and using Spring's ShallowEtagHeaderFilter to set etag to my resources but I need to also add the cache-control headers.

like image 745
Paul Avatar asked Jul 13 '10 07:07

Paul


1 Answers

Use mvc:resources for static files and mvc:interceptors with WebContentInterceptor for non-static files e.g.

  <!-- cache for one month -->
  <mvc:resources location="/css/" mapping="/css/**" cache-period="2592000"/>

  <!-- don't send any cache headers, rely on last-modified timestamps only -->
  <mvc:resources location="/img/" mapping="/img/**"/>
  <mvc:resources location="/js/" mapping="/js/**"/>

  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/**/*.htm" />
        <bean id="responseCachingFilter" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
          <property name="cacheSeconds" value="0" />
          <property name="useExpiresHeader" value="true" />
          <property name="useCacheControlHeader" value="true" />
          <property name="useCacheControlNoStore" value="true" />
          <property name="cacheMappings">
          <props>
            <!-- cache for one month -->
            <prop key="/**/*.htm">2592000</prop>
          </props>
        </property>
      </bean>
    </mvc:interceptor>
  </mvc:interceptors>
like image 193
Stevo Slavić Avatar answered Nov 15 '22 09:11

Stevo Slavić