2011-01-11 32 views
6

我在我的web應用程序的根目錄結構的彈簧3 MVC調度servlet和使用MVC:資源如文檔描述提供靜態內容: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources允許緩存與Spring MVC的MVC:資源標籤

谷歌的Chrome瀏覽器審計告訴我,這些資源顯式不可緩存。

Cache-Control:max-age=31556926, must-revalidate 
Content-Length:1022 
Content-Type:image/png 
Date:Tue, 11 Jan 2011 00:20:07 GMT 
Expires:Wed, 11 Jan 2012 06:08:53 GMT 
Last-Modified:Mon, 29 Nov 2010 19:53:48 GMT 

所以,我需要什麼,以使資源緩存:這裏有相同的瀏覽器說,頭與響應發送?

+0

你設置的MVC緩存週期屬性:資源進入你的應用程序配置文件? – DwB 2011-01-11 19:52:08

+0

是的,你可以看到在Cache-Control的最大時間裏設置的值。今晚我要安裝Spring的ETag過濾器,看看是否能解決這個問題。 – digitaljoel 2011-01-11 21:17:01

+0

ETag過濾器是否解決了這個問題?我有同樣的問題。 – les2 2011-08-02 23:26:51

回答

1

也許org.springframework.web.servlet.mvc.WebContentInterceptor可以幫到你嗎?只需將它添加到攔截列表:

<mvc:interceptors> 
    <bean class="org.springframework.web.servlet.mvc.WebContentInterceptor"> 
     <property name="cacheMappings"> 
      <props> 
       <prop key="/ajax/promoCodes">300</prop> 
       <prop key="/ajax/options">0</prop> 
      </props> 
     </property> 
    </bean> 
</mvc:interceptors> 
3

由於Spring框架4.2,this is now fixed with more flexible Cache-Control header values的。

"must-revalidate"值現在默認情況下禁用,你甚至可以寫這樣的事:

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**") 
       .addResourceLocations("/static/") 
       .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS).cachePublic()); 
    } 

}