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<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/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">
    <!--组件扫描-->
    <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/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文件为:

<?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 结语

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

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

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值