簡體   English   中英

Spring v3沒有為元素'mvc:resources'找到聲明

[英]Spring v3 no declaration can be found for element 'mvc:resources'

目前正在運行

Tomcat:v6

Spring Tools Suite: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:匹配的通配符是strict,但是沒有找到元素'mvc:annotation-driven'的聲明。 app-servlet.xml / app / www / WEB-INF

  • cvc-complex-type.2.4.c:匹配的通配符是strict,但是沒有為元素'mvc:resources'找到聲明。 app-servlet.xml / app / www / WEB-INF

已知的問題

如何修復編譯錯誤以使mvc:資源正常工作?

我一直在挖掘大約2個小時,還沒有可靠的答案......

在您的spring上下文中,xml mvc namespace 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

在我的彈簧配置文件中,我有這兩個網址

    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 ie ,spring-mvc-4.2.xsd和spring-tx-4.2.xsd

因此,在我看來,明確指定版本是一個很好的做法。 它對我有用,希望這也適合你。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM