Spring v3 找不到元素 'mvc:resources' 的声明



当前运行

Tomcat: v6

Spring Tools Suite: v2.7.2

Spring Framework: 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映射静态文件。

<<h2> bug/h2>
  • cvc-complex-type. 2.4.4 c:匹配的通配符是严格的,但是没有为元素'mvc:annotation-driven'找到声明。app-servlet.xml/app/www/web - inf

  • cvc-complex-type. 2.4.4 c:匹配的通配符是严格的,但是没有为元素'mvc:resources'找到声明。app-servlet.xml/app/www/web - inf

已知问题
  • http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd不包含元素资源

  • 如果替换为http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd仍然不工作,可能无法根据spring 3.05的jar文件工作

  • mvc:资源出现在spring v3.0.4,但没有新的xsd

我如何修复编译错误,以获得mvc:资源正确工作?

我已经挖了大约2个小时了,还没有确切的答案…

在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名称空间url时,我通常不使用版本信息大多数时候都很有效。您可以尝试使用名称空间url

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

代替

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

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

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

我认为您的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命名空间的第一行,它应该工作得很好

我报名参加了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 and spring-tx-4.2.xsd

所以,在我看来,明确指定版本号是一个很好的做法。这对我有用,希望这对你也有用。谢谢你。

最新更新