mvc:annotation-driven不受约束

时间:2011-05-14 11:42:10

标签: spring

当我在NetBeans中运行某个Spring Web 3项目时出现此错误:

  

org.xml.sax.SAXParseException;   lineNumber:11; columnNumber:30;该   元素的前缀“mvc”   “mvc:annotation-driven”不受约束。

这是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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="net.myproject.controllers"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>

我认为我做了相应的命名空间声明,但显然我仍然忽略了一些东西。为什么我会收到此错误?

7 个答案:

答案 0 :(得分:38)

这是一个IDE错误,可以通过以下

来解决
  

的xmlns:MVC = “http://www.springframework.org/schema/mvc”

enter image description here

答案 1 :(得分:26)

试试这对我有用:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven />

答案 2 :(得分:11)

它应该像那样工作,它对我有用。问题可能出在IDE / xml编辑器中。尝试忽略xml错误并运行应用程序。

答案 3 :(得分:3)

这是一个IDE错误,可以通过将以下内容添加到Spring Dispatcher Servlet

来解决

的xmlns:MVC =&#34; HTTP://www.springframework.org/schema/mvc"

如果您的xml文件中未包含以下代码段,请添加以下代码段

的xsi:的schemaLocation =&#34; HTTP://www.springframework.org/schema/mvc                     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd&#34;

答案 4 :(得分:1)

您可能遗漏了命名空间声明的一些重要部分,如下所述:http://forum.springsource.org/showthread.php?100397-The-prefix-mvc-for-element-mvc-annotation-driven-is-notbound

它不会影响运行时,但开发环境会遇到与您描述的类似问题。

我想应该这样: http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

答案 5 :(得分:1)

添加: 的xmlns:P =&#34; HTTP://www.springframework.org/schema/p"

否则xml将无法理解p注释。

答案 6 :(得分:1)

更改下面的代码对我有用

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

到下面的代码(imp:检查xmlns:context和xmlns:xsi位置)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">