6

独自のフィルターを作成しなくても、静的リソースにこれらのキャッシュヘッダーを設定できるようにする、ボックスのキャッシュ制御応答ヘッダーフィルターの権利はありますか?それはそのような一般的な仕事のようです。スプリングフィルターはありますか?現在Tomcat6.0を使用しており、SpringのShallowEtagHeaderFilterを使用してetagをリソースに設定していますが、cache-controlヘッダーも追加する必要があります。

4

3 に答える 3

12

静的ファイルには mvc:resources を使用し、非静的ファイルには mvc:interceptor を 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);
        }
...

web.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の独自の impl を指します。WebContentGenerator は、Spring を使用して DelegatingFilterProxy に依存性注入されます。また、 impl はFilterを実装し、doFilter から WebContentGenerator の適切なキャッシュ設定メソッドを呼び出します。

于 2010-07-14T11:31:31.240 に答える