簡體   English   中英

使用Spring MVC,Tomcat應用程序為靜態資源啟用瀏覽器緩存

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

我在應用程序中使用Tomcat-7, Spring-4, Hibernate-4 我嘗試了兩種方法來使caching起作用,但是當我使用gtmetrix檢查時,它們似乎gtmetrix

使用.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>

使用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>


我可以將Tomcat, Spring-MVC使用哪些其他方法來使browser caching正常工作?
請分享您的經驗。

另一種方法是這樣的:

<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