Spring WebContentInterceptor没有添加cacheSeconds到标题
问题描述:
我想在spring mvc中添加配置,以便我的静态内容像js,图像将被浏览器缓存。我在调度程序中添加了以下内容-servlet.xmlSpring WebContentInterceptor没有添加cacheSeconds到标题
<beans>
.............
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="31556926"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
</beans>
但是我仍然看不到缓存已启用。我在浏览器调试器中看到以下内容:Cache-Control:“no-cache”。
请帮助!
答
您可以使用mvc:资源来缓存静态文件。
<mvc:resources mapping="/static/**" location="/public-resources/"
cache-period="31556926"/>
<mvc:annotation-driven/>
答
您错过了cacheMappings
property来配置路径模式/缓存指令。
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="31556926"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
<property name="cacheMappings">
<props>
<prop key="/static/**">2592000</prop>
</props>
</property>
</bean>
但的确,Burak Keceli的答案是现货;您最好使用WebMvcConfigurerAdapter.addResourceHandlers
或使用<mvc:resources/>
的XML配置版本。
答
我有同样的问题。我添加了拦截器,但在HTTP响应中注意到了这一点。
在我的情况下,在我的项目中有另一个弹簧上下文XML,其中另一个<mvc:interceptors>
已注册。它有效地取代了我的WebContentInterceptor配置。结合他们解决了它。
也许类似的东西在你的配置阿卜杜勒·萨马德是怎么回事...
我曾经尝试都被布拉克Keceli和@布赖恩Clozel,规定的方法,并没有他们的工作。我以某种方式没有得到缓存控制头的值,很奇怪。 目前我已经在dispatcher-servlet.xml中指定了这个设置以及上下文。我应该在一个单独的bean xml中指定它吗? –