2015-11-04 29 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文件中的每个资产之后。