当前位置: 首页 > news >正文

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 {@Autowiredprivate TeacherService teacherService;/*** 请求地址:/teacher/list* @return 老师列表*/@RequestMapping("/list")public List<Teacher> getTeacherList(){List<Teacher> teacherList = teacherService.getTeacherList();return teacherList;}
}

配置文件spring-mvc.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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/contex/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--组件扫描--><context:component-scan base-package="org.example.*" /><!--注解驱动,能够进行消息转换,返回JSON数据--><mvc:annotation-driven><mvc:message-converters><bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"><property name="defaultCharset" value="utf-8" /><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value><value>application/json</value></list></property></bean></mvc:message-converters></mvc:annotation-driven><!--事文件解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="UTF-8" /></bean><!--事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!--事务注解驱动器--><tx:annotation-driven/><!--mvc默认处理器 --><mvc:default-servlet-handler />
</beans>

其中,从事后的角度讲,上述配置文件出问题的是如下的xsi:schemaLocation内容:

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/contex/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/txhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://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文件为:

<?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"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/context/spring-context.xsd"><!--读取数据库配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!--数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${driverClassName}" /><property name="url" value="${url}" /><property name="username" value="${user}"/><property name="password" value="${pwd}" /></bean><!--SqlSession工厂对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath:mapper/*.xml" /><property name="plugins" ><array><bean class="com.github.pagehelper.PageInterceptor"></bean></array></property><property name="configuration"><bean class="org.apache.ibatis.session.Configuration" ><property name="mapUnderscoreToCamelCase" value="true" /></bean></property></bean><!--mapper包扫描器 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.mapper" /></bean>
</beans>

运行后,会自动弹出“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 结语

遇到问题,要敢于解决。培训老师留下了好多空白,需要自己课下摸索。因为培训和平时上课很不一样。培训一般要求是在短时间内讲授大量的内容。

所以参加培训,课下加强编程实践锻炼很有必要。

http://www.lryc.cn/news/317968.html

相关文章:

  • MySQL事务隔离
  • Java基础知识总结(1)
  • 脚手架原理之webpack处理html文件和模块打包
  • Winform编程详解一:Form窗口
  • Windows Server 2025 Install Preview
  • 四、MySQL
  • C#使用泛型自定义的方法设计队列CQueue<T>类
  • IDEA自定义Maven仓库
  • Codeql复现CVE-2018-11776学习笔记
  • CVE-2024-27199 JetBrains TeamCity 身份验证绕过漏洞2
  • ms office学习记录12:Excel学习记录㈥
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的条形码二维码检测系统(深度学习+UI界面+训练数据集+Python代码)
  • npm yarn 一起使用报错
  • 基于springboot实现驾校信息管理系统项目【项目源码+论文说明】计算机毕业设计
  • DXP软件界面显示“No Hard Devices”【简单的操作问题】加【软件下载】
  • 通过Spring Boot 实现页面配置生成动态接口?
  • 【数据结构与算法】:插入排序与希尔排序
  • 前端性能优化——javascript
  • Docker容器化技术(使用Docker搭建论坛)
  • C# ListView 控件使用
  • 【string一些函数用法的补充】
  • 【Go】令牌桶限流算法
  • go的slice学习
  • 软件设计师17--磁盘管理
  • 学点Java打小工——Day2Day3一点作业
  • 【话题】2024年AI辅助研发趋势,有那些应用领域
  • 蓝桥杯——数组切分
  • 【机器学习】进阶学习:详细解析Sklearn中的MinMaxScaler---原理、应用、源码与注意事项
  • 数据库是什么?数据库连接、管理与分析工具推荐
  • 【C#算法实现】可见的山峰对数量