【问题标题】:Spring v3 no declaration can be found for element 'mvc:resources'Spring v3 找不到元素“mvc:resources”的声明
【发布时间】:2011-11-18 04:05:50
【问题描述】:

正在运行

Tomcat:v6

Spring 工具套件:v2.7.2

Spring 框架:spring-webmvc-3.0.5

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: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/mvc/spring-mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <mvc:annotation-driven />

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

      <context:component-scan base-package="com.app.mvc" />

 </beans>

web.xml 部分代码

<servlet-mapping>
    <servlet-name>duckapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Servlet 用途

web.xml 将所有 url 映射到 servlet,但 mvc:resources 映射静态文件除外。

错误

  • cvc-complex-type.2.4.c:匹配的通配符是严格的,但找不到元素“mvc:annotation-driven”的声明。 app-servlet.xml /app/www/WEB-INF

  • cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素“mvc:resources”的声明。 app-servlet.xml /app/www/WEB-INF

已知问题

问题

如何修复编译错误以使 mvc:resources 正常工作?

我已经为此挖掘了大约 2 个小时,还没有可靠的答案......

【问题讨论】:

  • 我不认为在 xsd 声明中使用版本是可取的。如果没有给出版本,Spring 将在项目的依赖项中使用最高版本。

标签: java xml spring servlets schema


【解决方案1】:

在您的 spring 上下文中,xml mvc 命名空间 url 应该与 schemaLocation 中的 url 匹配。像这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

这是一个标准的 XML 命名空间声明。命名空间 url 是一种唯一的 id,然后映射到 xsi:schemaLocation 中的实际模式位置。

【讨论】:

  • 谢谢,这正是问题所在!
  • 谢谢,也为我工作。我希望这在 Spring 中有更好的记录
  • 不起作用。我有完全相同的问题,但使用 spring 3.1
  • 在上面的 sn-p 中你唯一需要改变的是 spring-mvc.xsd 的版本 - springframework.org/schema/mvc/spring-mvc-3.1.xsd
  • 在这种情况下尝试以下一个 xmlns:beans="springframework.org/schema/beans" 我遇到了同样的问题,这解决了它..
【解决方案2】:

当使用 Spring 命名空间 url 时,我通常不使用版本信息,并且 大多数时候工作得很好。 您可能想尝试命名空间 url

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

而不是

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

【讨论】:

  • 我不认为在 xsd 声明中使用版本是可取的。如果没有给出版本,Spring 将在项目的依赖项中使用最高版本。
【解决方案3】:

我遇到了同样的错误。原因是缺少 Maven 依赖项 spring -webmvc。我包含了以下依赖项,它开始工作了。

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

【讨论】:

    【解决方案4】:

    我认为您的 schemaLocation 映射不正确。命名空间指定为:

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    

    这是正确的,我相信,但在 schemaLocation 你有

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

    因此,如果您将 schemaLocation 映射的第一行更改为您的 mvc 命名空间,它应该可以正常工作。

    【讨论】:

      【解决方案5】:

      我已报名参加 udemy 的春​​季课程。我按照老师教我做的每一个步骤去做。 所以如果你使用spring mvc和hibernate你可能会遇到这个错误 无法读取架构文档“http://www.springframework.org/schema/tx/spring-tx.xsd”等:

      <mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> elements
      

      在我的 spring 配置文件中我有这两个 url

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

      在 xsi:schemaLocation 中,我将其替换为

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

      实际上我访问了这两个站点 http://www.springframework.org/schema/mvc/http://www.springframework.org/schema/tx/ 并且刚刚添加了最新版本的 spring-mvc 和 spring-tx,即 spring-mvc-4.2.xsd 和 spring-tx-4.2.xsd

      所以,在我看来,明确指定版本 no 是一个好习惯。 它对我有用,希望这对你也有用。 谢谢。

      【讨论】:

        猜你喜欢
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2017-09-17
        • 1970-01-01
        • 2012-07-02
        • 2012-11-28
        • 2012-09-14
        相关资源
        最近更新 更多