2012-06-26 64 views
3

我想配置Spring MVC有以下方案:聯合MVC:攔截器和MVC:資源只緩存靜態資源

  • 開頭的任何URL「{}背景/資源」應該是緩存
  • 任何其他網址不應爲緩存(在我的情況下,通過「資源」是動態的所有內容)

,第一部分是很容易的使用MVC:資源:

<mvc:resources mapping="/resources/**" location="..." cache-period="3600"/> 

我有點失落的第二部分。 默認情況下,它看起來像「動態資源」沒有緩存相關的信息(沒有緩存控制頭,沒有編譯指示等)。有些瀏覽器可能會緩存一些內容(FF),有些瀏覽器可能無法緩存(Chrome),這種內容可能並不具備。

大約有一個如何使用和WebContentInterceptor的combinason做一些網頁很多職位可緩存:

<mvc:interceptors> 
    <mvc:interceptor> 
     <mvc:mapping path="/foobar/**"/> 
     <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> 
      <property name="cacheSeconds" value="0"/> 
      <property name="useExpiresHeader" value="true"/> 
      <property name="useCacheControlHeader" value="true"/> 
      <property name="useCacheControlNoStore" value="true"/> 
     </bean> 
    </mvc:interceptor> 
</mvc:interceptors> 

但在我而言,這是不可用作這樣的,因爲我沒有一個路徑表達匹配(排除是不可能的)。

那麼有什麼辦法可以表達這一點: *做什麼mvc:資源對資源做的事情 *對所有其他頁面做「某事」?

我能看到的唯一選擇是編寫一個自定義攔截器來檢查是否somehing是資源,但這聽起來有點奇怪,不能全局定義緩存屬性。

謝謝

回答

1

Spring MVC的行爲就像你想要的那樣。 WebContentInterceptor截取並將緩存控制僅添加到文本/ html資源的請求中。

做所有URL的方式就是離開< MVC:測繪>從配置中:

<mvc:interceptors> 
    <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> 
     <property name="cacheSeconds" value="0"/> 
     <property name="useExpiresHeader" value="true"/> 
     <property name="useCacheControlHeader" value="true"/> 
     <property name="useCacheControlNoStore" value="true"/> 
    </bean> 
</mvc:interceptor> 

同時,由於< MVC:攔截> & < mvc:資源>是獨立的,任何特定的t軟管兩個標籤並不重要(與答案here中的建議不同)。