2011-01-11 86 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()); 
    } 

}