首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Spring MVC 配置 報錯:Namespace 版本不匹配解決

     編輯:程式設計疑難解答

報錯信息

Referenced file contains errors 
(https://www.springframework.org/schema/beans/spring-beans-4.0.xsd).

刪除 <mvc:annotation-driven/> 後不報錯。

根因

Spring 配置文件頂部 <beans ...> 的 xsi:schemaLocation 中,namespace 版本號與項目實際引用的 Spring JAR 版本不一致。
例如:項目用 Spring 5.3.x,但 XML 寫死 spring-beans-4.0.xsd,MyEclipse 驗證時下載 4.0 XSD 校驗,發現 mvc:annotation-driven 在 4.0 中定義不同或缺失,報錯。

修正方法

  1. 打開 Spring 配置 XML。
  2. 將所有 xsi:schemaLocation 中的版號統一為項目實際版本(或去掉版號用「無版本」模式)。
  3. 推薦寫法(無版本,自動匹配 classpath 中的最高版本 XSD):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="https://www.springframework.org/schema/context"
       xmlns:mvc="https://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
           https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
           https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="com"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

MyEclipse 專屬操作

  • 右鍵 XML → Validation → 勾選 Use namespace version from project libraries。
  • 或在 Preferences → XML → XML Catalog 添加本地 XSD 映射。

總結

Spring XML 的 schemaLocation 版號必須與項目依賴的 Spring 版本一致,建議直接寫無版本路徑(spring-beans.xsd)讓容器自動匹配。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved