如何使用Tomcat启用浏览器对静态内容(图像,css,js)的缓存?


问题内容

如何使用Tomcat启用浏览器对静态内容(图像,css,js)的缓存?最好的解决方案是编辑spring MVC配置文件或web.xml


问题答案:

尝试(更改值)

<mvc:resources mapping="/static/**" location="/public-resources/" 
       cache-period="31556926"/>
<mvc:annotation-driven/>

您还可以使用拦截器:

<mvc:interceptors>
   <mvc:interceptor>
    <mvc:mapping path="/static/*"/>
    <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:interceptor>
</mvc:interceptors>

请参阅MVC文档