2015-11-04 44 views
0

我想使我的資產文件夾基礎依賴於版本,以防止爲我的用戶緩存問題。我目前使用spring和servlet.xml來配置我的靜態路由。如何在spring-servlet.xml中的靜態內容路徑中使用變量

這是我目前的配置

<!-- Static routes --> 
    <mvc:resources mapping="/assets/**" location="/assets/" cache-period="0" /> 
    <mvc:resources mapping="/img/**" location="/assets/img/" cache-period="31556926" /> 
    <mvc:resources location="/assets/img/favicon.ico" mapping="/favicon.ico" /> 

我想用這樣的事情,但我不知道如何配置這一點。

<!-- Static routes --> 
    <mvc:resources mapping="/assets/${version}/**" location="/assets/" cache-period="0" /> 
    <mvc:resources mapping="/img/**" location="/assets/${version}/img/" cache-period="31556926" /> 
    <mvc:resources location="/assets/${version}/img/favicon.ico" mapping="/favicon.ico" /> 

version變量在application.properties中定義,我成功地在我的JSP文件中使用它。但我註冊我的靜態路由似乎不能使用它。請指教。

+0

我認爲Spring MVC已經構建了資源緩存清除的功能。看看[這篇文章](http://stackoverflow.com/a/30139512/1291150)或[這一個](https://spring.io/blog/2014/07/24/spring-framework-4 -1-handling-static-web-resources),然後嘗試實現一些自定義解決方案。免責聲明:我沒有親自嘗試過,但它可以幫助你。 [This SO post](http://stackoverflow.com/a/9204940/1291150)如果您決定使用自定義解決方案,則包含對您的問題的解答(mvc:resources中的佔位符)。祝你好運! –

+0

謝謝,聽起來不錯。雖然我可能仍然使用自定義解決方案來運行,但是如果我能夠按照版本提供的應用程序的想法工作,那麼我可以運行它。 –

回答

0

我結束了使用下面的語法來捕捉版本字符串並相應地映射。

在攔截器:

<mvc:interceptor> 
    <mvc:mapping path="/assets/{version}}/**" /> 
    <bean id="webContentInterceptor" 
     class="org.springframework.web.servlet.mvc.WebContentInterceptor"> 
    <property name="cacheSeconds" value="3600" /> 
    <property name="useExpiresHeader" value="true" /> 
    <property name="useCacheControlHeader" value="true" /> 
    <property name="useCacheControlNoStore" value="false" /> 
    </bean> 
</mvc:interceptor> 

在資源映射

<mvc:resources mapping="/assets/{version}/**" location="/assets/" cache-period="0" /> 

版本括號將捕獲任何子路資產從而使我使用的版本映射可能只是通過把一個版本在我的jsp文件中的每個資產之後。

相關問題