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

标签 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。
如果有人能帮助找出我的错误,我将不胜感激。

最佳答案

是的,所以我的一所大学已经解决了这个问题。 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>

关于java - 基于配置 xml 的资源版本控制(Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087131/

相关文章:

java - Spring @Scheduled 注解方法在给定时间内运行多次

java - 无法启动 Apache TomEE 服务器 - 未找到部署 : openejb/Deployer

java - 我们如何决定在maven中使用哪个java版本来编译一个版本的库?

java - Scala 不会在循环内将 Element 转换为产生值

java - 如何在 Spring Boot 中设置 useBodyEncodingForURI ="true"

Spring MVC 表单不采用对象路径

java - Java 的哪个版本引入了匿名类?

java - 如何使用modelAttribute在ajax(jquery)中提交spring表单

spring - 注入(inject)用 Spring 扩展抽象类的类列表

java - 错误 javax.validation.ConstraintViolationException