1.前言
今天跟着老师的视频学习如何用Tomcat+SpringBoot写一个简单的接口界面,不过还是无一例外的项目报错(已经非常习惯了),接着又是网上一通乱找(唉,小白好难啊),最后还是自己解决了。
2.问题
报错如下:
严重: Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [applicationcontext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 29; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘mvc:annotation-driven’ 的声明。
接着我就去相应的XML文件去寻找错误,发现我的头文件的有些约束竟然是灰色的(就是没有用到)
3.解决办法
将下面的代码替换该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: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">
如果annotation-driven爆红了,就用鼠标放到爆红的地方,如何声名这个属性
最后再检查一下有没有一下这行代码,如果没有就要加进去,因为它是起到解析MVC的作用不可缺失。
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
最后再运行项目发现项目就可以运行了,开学。
最后希望能帮助到你,谢谢你这么优秀,还关注我。