行业知识
SSM Java Web项目由于spring-mvc.xml配置不对带来的一系列问题
1 介绍
一年多前,���就买了好多关于Java开发类的书籍,内容关于Java Web实操、Spring 学习指南、Maven实战、IntelliJ IDEA软件开发与应用等等。可是由于工作繁忙,这些书没系统地看完。这也是参加工作后的无奈吧!
寒假期间的一周(从2024年1月22日–26日),参加了学校组织的一个免费的Java架构师培训。培训的内容包括传统Java Web开发、各种框架的介绍、SSM 项目开发、Spring Boot项目开发、微服务等,很丰富。自己对这些内容很感兴趣,也愿意学,无奈当时工作忙,临近期末批改期末大报告的关键期,没认真听。只好现在重现复盘、学习培训的内容。
2 问题表现及解决方法
其中有一个SSM Java Web项目,我完全按照培训师给出的代码,无法得到正确网页展现。从事后回顾来说,涉及到项目的spring-mvc.xml文件。具体描述如下。
代码文件TeacherController.java用于指定要访问的网址“/teacher/list”,从而能让网页返回一个从MySQL数据库中读取的teacher表信息,其代码如下:
package org.example.controller;import org.example.pojo.Teacher;import org.example.service.TeacherService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController@RequestMapping("/teacher")public class TeacherController { @Autowired private TeacherService teacherService; /** * 请求地址:/teacher/list * @return 老师列表 */ @RequestMapping("/list") public List getTeacherList(){ List teacherList = teacherService.getTeacherList(); return teacherList; }} 配置文件spring-mvc.xml的内容如下:
text/html;charset=UTF-8 application/json
其中,从事后的角度讲,上述配置文件出问题的是如下的xsi:schemaLocation内容:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/contex/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
2.1 问题表现1及解决
问题表现1
按照上面配置,运行项目(在IDEA中点击启动Tomcat服务器的按钮),能运行,且自动弹出了页面,如下:
但当我访问 http://localhost:8080/teacher/list 时,出现如下图:
读取不到MySQL中的teacher表信息。
上面页面提示信息与Tomcat server的执行日志信息是一样的,核心问题为:
14-Mar-2024 11:04:00.528 警告 [RMI TCP Connection(2)-127.0.0.1] org.springframework.util.xml.SimpleSaxErrorHandler.warning Ignored XML validation warning
org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 60; schema_reference.4: 无法读取方案文档 ‘https://www.springframework.org/schema/contex/spring-context.xsd’, 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 xsd:schema。
…
14-Mar-2024 11:04:00.537 严重 [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from file [D:\RBprogramming\StudySSM\ssm_demo\target\web-ssm_demo\WEB-INF\classes\spring-mvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 60; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明。
…
Caused by: org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 60; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明。
问题1解决
更改配置文件spring-mvc.xml中的xsi:schemaLocation的值为:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"
亦即将spring-beans.xsd改为 spring-beans-4.2.xsd,spring-context.xsd改为spring-context-4.2.xsd,spring-mvc.xsd改为spring-mvc-4.2.xsd。
再次运行。可以发现,问题1的表现不再出现。但出现了新的问题。
2.2 问题表现2及解决
问题表现2
运行上面修改后的项目,仍会自动弹出"Hello World!"页面,但访问 http://localhost:8080/teacher/list 时,出现如下图:
从Tomcat server的执行日志信息中取出核心问题描述:
14-Mar-2024 12:02:58.706 严重 [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 40 in XML document from file [D:\RBprogramming\StudySSM\ssm_demo\target\web-ssm_demo\WEB-INF\classes\spring-mvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 40; columnNumber: 28; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘tx:annotation-driven’ 的声明。
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:402)
…
Caused by: org.xml.sax.SAXParseException; lineNumber: 40; columnNumber: 28; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘tx:annotation-driven’ 的声明。
at com.sun.org.apache.xerces.internal.util.ErrorH
问题2解决
根据问题2的表述及与原始xsi:schemaLocation内容的对比,我认为在xsi:schemaLocation的值中应该追加如下内容:
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
最终正确的完整的spring-mvc.xml文件为:
运行后,会自动弹出“Hello World!”页面,但是,再次访问http://localhost:8080/teacher/list,服务器的执行log中无限循环报错。
2.3 问题表现3及解决
问题表现3
出现的问题核心描述为:
java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
问题3解决
将jdbc的连接设为:
url=jdbc:mysql://localhost:3306/ssm_demo?useSSL=false&allowPublicKeyRetrieval=true
再次运行,访问http://localhost:8080/teacher/list,得到:
能读取到数据库中的信息了。
3 结语
遇到问题,要敢于解决。培训老师留下了好多空白,需要自己课下摸索。因为培训和平时上课很不一样。培训一般要求是在短时间内讲授大量的内容。
所以参加培训,课下加强编程实践锻炼很有必要。
上一篇:164邮箱服务器怎么填
下一篇:怎么配置apache2服务器?