我已经在我的webapp的根目录下配置了Spring 3 MVC Dispatcher servlet,并使用mvc:resources来提供静态内容,如文档中所述: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
Google的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 :(得分:3)
从Spring Framework 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());
}
}
答案 1 :(得分: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>