基于配置xml的资源版本控制(Spring)

时间:2016-06-28 22:11:36

标签: java spring spring-mvc xml-parsing

我正在尝试使用VersionResourceResolver以防止缓存旧的js和css。目前我正在为我的Spring使用基于XML的配置。我刚刚复制了这段代码:

<mvc:resources mapping="/resources/**" location="/public-resources/">
    <mvc:resource-chain>
        <mvc:resource-cache/>
        <mvc:resolvers>
            <mvc:version-resolver>
                <mvc:content-version-strategy patterns="/**"/>
            </mvc:version-resolver>
        </mvc:resolvers>
    </mvc:resource-chain>
</mvc:resources>

并将其放在DispatcherServlet调用的xml中,以启动我的Web应用程序。将此代码放入我的servlet.xml后,我有这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">



    <context:property-placeholder location="/WEB-INF/application.properties" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <mvc:resources mapping="/resources/**" location="/resources/">
        <mvc:resource-chain>
            <mvc:resource-cache />
            <mvc:resolvers>
                <mvc:version-resolver>
                    <mvc:content-version-strategy patterns="/**"/>
                </mvc:version-resolver>
            </mvc:resolvers>
        </mvc:resource-chain>
    </mvc:resources>

    <mvc:resources mapping="/sources/**" location="/sources/" />

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
        <property name="proxyTargetClass" value="true" />
    </bean>
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="pt_BR" />
    </bean>
</beans>

Eclipse告诉我,我的servlet.xml中有2个错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'mvc:resource-cache'. One of '{"http://www.springframework.org/schema/mvc":resolvers, "http://www.springframework.org/schema/mvc":transformers}' is expected.
cvc-complex-type.4: Attribute 'resource-cache' must appear on element 'mvc:resource-chain'.

我不知道自从我使用mvc.xsd的最后一个版本后会发生什么 如果有人能帮忙找到我的错误,我很感激。

1 个答案:

答案 0 :(得分:3)

是的,所以我的一所大学已经解决了这个问题。 Spring documentation是错误的,使用基于XML的配置的资源版本控制的正确方法是:

<mvc:resources mapping="/resources/**" location="/public-resources/">
    <mvc:resource-chain resource-cache="true">
        <mvc:resolvers>
            <mvc:version-resolver>
                <mvc:content-version-strategy patterns="/**"/>
            </mvc:version-resolver>
        </mvc:resolvers>
    </mvc:resource-chain>
</mvc:resources>