(八)日常找错

  • 今天遇到了一个之前这样用都没有发生错误的错误,我还是将它记录下来吧。

  • 项目背景

  • 在ssm项目整合时候,想实现从web目录下的index.jsp文件,点击里面的超链接跳转到web/pages/register.jsp页面,然后在里面完成学生注册功能,当点击注册提交按钮,通过StudentController控制器跳转到WEB-INF目录下的result.jsp页面输出结果。

在这里插入图片描述

  • index,jsp页面如下:
<%--
  Created by IntelliJ IDEA.
  User: 小新
  Date: 2021/6/21
  Time: 11:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>首页</title>
</head>
<body>
<h3 align="center" style="color: dodgerblue">欢迎光临!</h3>
    <div align="center">
        <img src="static/images/mm.jpg" height="300" width="400">
        <table>
            <tr>
                <td><a href="pages/register.jsp">注册学生</a></td>
            </tr>
            <tr>
                <td>查询学生</td>
            </tr>
        </table>
    </div>
</body>
</html>
  • register.jsp页面如下:
<%--
  Created by IntelliJ IDEA.
  User: 小新
  Date: 2021/6/21
  Time: 23:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<html>
<head>
    <base href="<%=basePath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>注册页面</title>
</head>
<body>
    <h3 align="center"style="color: dodgerblue">注册页面</h3>
    <div align="center" style="background: aliceblue">
        <%--加了/开头就不用加base标签,否则使用base标签--%>
        <form action="student/register" method="post">
            <table>
                <tr>
                    <td>姓名:</td>
                    <td><input type="text" name="name"></td>
                </tr><tr>
                    <td>年龄:</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td><input type="submit" value="注册" style="color: dodgerblue"></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

  • result.jsp页面如下:
<%--
  Created by IntelliJ IDEA.
  User: 小新
  Date: 2021/6/21
  Time: 23:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <title>注册结果页面</title>
</head>
<body>
    <h3 align="center"style="color: dodgerblue">注册结果页面</h3>
    <div align="center" style="background: aliceblue">
       注册结果为:${tips}
    </div>
</body>
</html>

  • StudentController控制器如下:
/**
 *@program: SSM
 *@description: Student的处理器
 *@author: XiaoXin
 *@create: 2021-06-21 23:26
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService service;
    //注册学生
    @RequestMapping("/register")
    public ModelAndView addStudent(Student student){
        ModelAndView modelAndView = new ModelAndView();
        String tips="注册失败!";
        //调用service处理student
        int addStudent = service.addStudent(student);
        if(addStudent>0){
            //注册成功
            tips="学生【"+student.getName()+"】注册成功!";
        }
        //添加数据
        modelAndView.addObject("tips",tips);
        //指定展示的结果页面
        modelAndView.setViewName("result");
        return modelAndView;
    }
}
  • 运行结果
    在这里插入图片描述
  • 点击注册学生链接
    在这里插入图片描述
  • 输入数据并点击注册按钮报错如下:
HTTP Status 500 - Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

在这里插入图片描述

  • 分析:
    状态码500错误,下面也提示说可能是数据库连接出现问题。
    那么,500错误的原因大概是啥?
    1、网站语法错误,例如代码冲突等,都会造成500错误。
    2、数据库连接错误。
    3、网站超负荷运载,服务器压力过大,造成服务器无法响应,返回HTTP500错误。
    4、文件引用和路径出错(例如未启用父路径)。
    5、DNS解析原因,云测速备案的时候,是屏蔽了国内IP的,造成我在百度站长工具测试自己网页的时候,百度站长工具提示500错误。

  • 解决思路(错误提示是在数据库连接出现问题,聪明的孩子可以直接去排查数据库连接等是否有问题,我这里不直接去)

  1. 检查网络连接是否正常
    可以直接在浏览器访问控制器给的地址,可以正常访问,排除。
  2. 检查文件路径是否正确
    web下的index.jsp是不能直接访问WEB-INF下的资源的,即客户端不能直接访问WEB-INF下的资源,需要服务器(我这里可以是自己的控制器)进行访问。经过检查,index,jsp访问的是web/pages/下的register.jsp文件,经过register.jsp文件中的<form action="student/register" method="post"> action动作,跳转到控制器,然后控制器的解析器解析下面操作modelAndView.setViewName("result"); return modelAndView; 进而访问的result.jsp页面,所以是不存在文件路径问题的,排除。
  3. 检查配置文件

spring配置文件

    <!--SqlSessionFactoryBean创建SqlSqlSessionFactory对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--声明MyBatis扫描器,创建mapper对象,配置扫描Dao接口包,动态实现Dao接口注入到spring容器中-->
    <!--详情可看:https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--将values中的sqlSessionFactory对象(由上面SqlSessionFactoryBean创建的,对应为id值)赋值给name值,-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--给出需要扫描的Mapper接口包,value值是项目中mapper包所在的位置,这样所有在mapper包下的对象都能自动创建出来-->
        <property name="basePackage" value="com.xxx.mapper"/>
    </bean>

mybatis配置文件:

  <!--一定要注册绑定,每一个mapper.xml都需要在Mybatis核心配置文件中配置(sql 映射文件),且名字和包名最好都相同-->
    <mappers>
        <mapper resource="com/xxx/mapper/StudentMapper.xml"/>
    </mappers>

mapper.xml文件:

<mapper namespace="com.xxx.mapper.StudentMapper">
    <!--由于配置了别名,这里不用resultType="com.xxx.pojo.Student">-->
    <select id="selectStudent" resultType="Student">
/*按照学号升序查询,建议写列名,不要写* */
        select id,name,age from student order by id asc
    </select>
    <insert id="insertStudent">
/*向学生表插入数据,由于id设置是自动增长的,只要插入其他列名即可*/
        insert into student(name,age) values (#{name},#{age})
    </insert>
</mapper>

可以发现,配置文件都没问题,所以排除。

  1. 检查数据库连接
    properties文件:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=输入自己的密码
jdbc.maxActive=10
jdbc.maxWait=3000

spring配置文件:

   <!--声明数据源,连接数据库-->
    <!--声明位置-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--声明数据源(druid)基本信息-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
    </bean>

没仔细看的话,好似也没啥问题,我的数据库使用的是8.0版本的,则jdbc.driverClassName=com.mysql.cj.jdbc.Driver 加上cj正确,用户名和密码也正确。于是我按照网上所说的,不使用properties文件看看行不行,于是我设置如下:

<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"/>
        <property name="username" value="root"/>
        <property name="password" value="自己的密码"/>
        <property name="maxActive" value="10"/>
        <property name="maxWait" value="3000"/>
    </bean>

注意点: url中的&连接符需要进行转义为:&amp 否则会报错。
当我换为这样后,还是报相同的错误,则说明也不是properties文件错误,而且,properties中的,也使用了前缀jdbc.xxx的形式,也不存在多个数据库冲突问题。
最后我注意到一点,就是数据库8.0以上的还要注意时区问题,我使用的是serverTimezone=UTC则会跟我们国家时区相差8小时(早于),这个之前使用不会出现问题,不知道这里怎么就出错了,而我注意到这个问题是在之前通过idea连接数据库时候发现的,如果想要在idea上连接数据库,我就需要设置时区为:serverTimezone=Asia/Shanghai才可以,使用UTC就不行,不知道大家都是这样,反正我遇到的是这样,然后打开数据库(可以本地也可以idea)

  1. 最终解决方案:
    将properties文件的jdbc.url中的serverTimezone=UTC替换为:serverTimezone=Asia/Shanghai成功解决。
    在这里插入图片描述

  2. 友情提示:在做web项目时候,要养成以下习惯:经常清理缓存;引入包时候,记得重新加载maven;不要一直沉迷复制错误然后网上直接找,不妨自己重新在心里理一下思路,自己跟自己对话一下,有时候问题就迎刃而解了,还有就是,当你试过很多方法还是不行时候,可以试试停下,关闭idea,下次打开时候,可能就可以了。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我实在是想不出什么好听的昵称了啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值