2015-11-05 22 views
1

我在我的应用程序中使用了Tomcat-7, Spring-4, Hibernate-4。我已经尝试了两种方法来使caching工作,但他们似乎没有工作,当我检查gtmetrix。使用.htaccess文件

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

方法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配置 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工作?
请分享你的经验。

+0

但是我已经接受了布拉克的答案,但我还是想知道的静态资源的''缓存更多的办法。 – Peter

回答

1

另一种方法是这样的:

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

谢谢@Burak。有效。顺便说一下,如何为不同的资源设置不同的cache-period,比如css cache-period将会是1个月,对于js,这个时间将会延长1年。任何想法 ? – Peter

相关问题