【问题标题】:Enable browser caching for static resources with Spring MVC,Tomcat application使用 Spring MVC、Tomcat 应用程序为静态资源启用浏览器缓存
【发布时间】:2015-11-05 07:19:54
【问题描述】:

我在我的应用程序中使用Tomcat-7, Spring-4, Hibernate-4。我尝试了两种方法来使caching 工作,但是当我检查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>

Approach-2 使用 Spring MVC xml 配置 mvc:interceptors :

<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 工作?
请分享您的经验。

【问题讨论】:

  • 但是我已经接受了 Burak 的回答,但我仍然有兴趣了解caching of static resources 的更多方法。

标签: java spring-mvc tomcat caching browser-cache


【解决方案1】:

另一种方法是这样的:

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

【讨论】:

  • 谢谢@Burak。有效。顺便说一句,我怎样才能为不同的资源设置不同的缓存周期,比如 css 缓存周期将是 1 个月,对于 js 它将是 1 年。有什么想法吗?
猜你喜欢
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 2011-09-25
  • 2014-09-26
相关资源
最近更新 更多