简体   繁体   English

使用Spring MVC,Tomcat应用程序为静态资源启用浏览器缓存

[英]Enable browser caching for static resources with Spring MVC,Tomcat application

I am using Tomcat-7, Spring-4, Hibernate-4 in my application. 我在应用程序中使用Tomcat-7, Spring-4, Hibernate-4 I have tried two approaches to make caching work but they don't seem to be working when I checked with gtmetrix . 我尝试了两种方法来使caching起作用,但是当我使用gtmetrix检查时,它们似乎gtmetrix

Approach-1 using .htaccess file: 使用.htaccess文件的方法1:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access 1 year"
  ExpiresByType image/jpeg "access 1 year"
  ExpiresByType image/gif "access 1 year"
  ExpiresByType image/png "access 1 year"
  ExpiresByType text/css "access 1 month"
  ExpiresByType application/pdf "access 1 month"
  ExpiresByType application/x-javascript "access 1 month"
  ExpiresByType application/x-shockwave-flash "access 1 month"
  ExpiresByType image/x-icon "access 1 year"
  ExpiresDefault "access 2 days"
</IfModule>

Approach-2 using Spring MVC xml Config mvc:interceptors : 使用Spring MVC xml Config mvc:interceptors方法2:

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


What are the other approaches that I can use with Tomcat, Spring-MVC to make browser caching work ? 我可以将Tomcat, Spring-MVC使用哪些其他方法来使browser caching正常工作?
Please share your experience. 请分享您的经验。

The other approach is this: 另一种方法是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM