移动<mvc:annotation-driven>后出错:找不到元素'mvc:annotation-driven'的声明</mvc:annotation-driven>

时间:2012-01-26 10:18:20

标签: java spring spring-mvc

我有一个dispatcher-servlet.xml和一个applicationContext.xml。

我一直在做一些重构,并且已经移动了

<mvc:annotation-driven/>
<context:component-scan base-package="com.xxx"/>

从dispatcher-servlet.xml到applicationContext.xml。

我现在收到此错误:

2012-01-26 10:34:36.434:WARN::Nested in org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.

我的applicationContext.xml如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"
       xmlns:context="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        >

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.xxx"/>

    <bean id="templateErrorListener"
          class="com.stringtemplate.log.Slf4jStringTemplateErrorListener"/>

    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
          p:config-location="/WEB-INF/ehcache.xml"/>

    <ehcache:annotation-driven cache-manager="ehCacheManager"/>

</beans>

和我的dispatcher-servlet.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
            </list>
        </property>
        <property name="ignoreAcceptHeader" value="true"/>
    </bean>

    <bean class="com.stringtemplate.StringTemplateViewResolver">
        <property name="templateErrorListener" ref="templateErrorListener"/>
        <property name="templateRoot" value="/WEB-INF/templates/"/>
        <property name="order" value="2"/>
    </bean>
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="3"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="formHttpMessageConverter"/>
                <ref bean="stringHttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <bean id="formHttpMessageConverter "
          class="org.springframework.http.converter.FormHttpMessageConverter "/>
    <bean id="stringHttpMessageConverter "
          class="org.springframework.http.converter.StringHttpMessageConverter "/>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

    <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:${systemTargetEnv}/app.properties</value>
                <value>classpath:/build.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName">
            <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
        </property>
    </bean>
</beans>

1 个答案:

答案 0 :(得分:7)

  

我一直在进行一些重构,并将<mvc:annotation-driven/>dispatcher-servlet.xml移到applicationContext.xml

你不想这样做。 <mvc:annotation-driven/>用于启用注释样式的MVC控制器,这些控制器必须位于dispatcher-servlet.xml中。将<mvc:annotation-driven/>放在applicationContext.xml中是没有意义的,它没有任何有意义的效果。

我应该回答这个问题,但答案是,当你声明了mvc命名空间时,你没有告诉Spring在哪里找到它的模式。添加

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

schemaLocation

中的applicationContext.xml属性
相关问题