2010-07-13 4 views
4

내 필터를 구축 할 필요없이 정적 리소스에서 캐시 헤더를 설정할 수있는 상자 캐시 제어 응답 헤더 필터가 있습니까? 그것은 일반적인 일처럼 보입니다. 스프링 필터가 있습니까? 현재 Tomcat 6.0을 사용 중이며 Spring의 ShallowEtagHeaderFilter를 사용하여 etag을 내 리소스에 설정하고 있지만 캐시 제어 헤더도 추가해야합니다.바로 캐시 제어 헤더 필터?

답변

2

DelegatingFilterProxy을 사용하여 캐시 헤더를 처리하기 위해 WebContentGenerator의 고유 한 impl을 지정합니다. WebContentGenerator는 Spring을 사용하여 DelegatingFilterProxy에 종속적으로 삽입됩니다. impl은 또한 Filter을 구현하고 doFilter에서 WebContentGenerator의 적절한 캐시 설정 메소드를 호출합니다. 바울의 솔루션에

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); 
     } 
... 

의 web.xml :

<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> 
+0

필터 매핑에는 하나의 URL 패턴 만있을 수 있습니다. responseCachingFilter bean이 확장을 기반으로하는 캐시 매핑을 지정하기 때문에 /*을 의미합니까? –

12
:
<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 - 서블릿) 애플리케이션 컨텍스트

정적 파일의 경우 mvc : resources를 사용하고 비 정적 파일의 경우 WebContentInterceptor와 함께 mvc : interceptors 사용

<!-- 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> 
관련 문제