6

是否有框缓存控制响应标头过滤器的权利,使我能够在我的静态资源上设置这些缓存标头而无需构建自己的过滤器?这似乎是一个常见的任务。有弹簧过滤器吗?我目前正在使用 Tomcat 6.0 并使用 Spring 的 ShallowEtagHeaderFilter 将 etag 设置为我的资源,但我还需要添加缓存控制标头。

4

3 回答 3

12

将 mvc:resources 用于静态文件,将 mvc:interceptors 与 WebContentInterceptor 用于非静态文件,例如

  <!-- 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>
于 2011-04-18T10:30:49.773 回答
5

保罗解决方案的详细信息:

 public class ResponseCachingFilter extends WebContentInterceptor implements
            Filter {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            try {
                this.preHandle((HttpServletRequest) request,
                        (HttpServletResponse) response, chain);
            } catch (Exception e) {
                throw new ServletException(e);
            }
            chain.doFilter(request, response);
        }
...

网页.xml:

<filter>
    <filter-name>responseCachingFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>responseCachingFilter</filter-name>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.css</url-pattern>
</filter-mapping>

并在(顶级,即不是 mvc-servlet)应用程序上下文中:

<bean id="responseCachingFilter" class="lala.ResponseCachingFilter">
    <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="/**/*.html">2592000</prop>
            <prop key="/**/*.htm">2592000</prop>
            <prop key="/**/*.jpg">2592000</prop>
            <prop key="/**/*.gif">2592000</prop>
            <prop key="/**/*.css">2592000</prop>
            <prop key="/**/*.js">2592000</prop>
        </props>
    </property>
</bean>
于 2011-04-07T16:57:24.830 回答
2

使用DelegatingFilterProxy,指向您自己的WebContentGenerator实现来处理缓存标头。使用 Spring 将 WebContentGenerator 依赖注入到 DelegatingFilterProxy 中。您的 impl 还将实现Filter并从 doFilter 调用 WebContentGenerator 的适当缓存设置方法。

于 2010-07-14T11:31:31.240 回答