Servlet中数据传到JSP页面使用el表达式${}无法显示问题

       当我使用mybatis从数据库查询数据,通过servlet返回到界面,出现了两种错误。首先看我的servlet和jsp页面。

Servlet代码

        List<Student> students = studentService.queryStu();//作用:查询学生信息   通过service层调用dao层的接口
        for(Student student:students)
            System.out.println(student);//遍历
        HttpSession session = request.getSession();///得到session对象  一次会话 
        session.setAttribute("students",students);//将集合存入session中
        System.out.println("存入==========");
        response.sendRedirect("show_stu.jsp");//重定向

JSP代码

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   //头部也导入jstl
<c:forEach items="${students}" var="stu">  <--遍历集合-->
            <tr>
                <td>${stu.studentNo}</td> <td>${stu.studentName}</td> <td>${stu.sex}</td>
                <td>${stu.age}</td><td>${stu.boreDate}</td><td>${stu.classNo}</td>
            </tr>
</c:forEach>

PS:studentNo,studentName...都为数据库和po层的字段


错误一:500服务器内部错误


服务器那边也查询出了学生信息


说明错误的位置和dao层及service没有关系,可能是页面和其他原因。

错误原因:仔细检查pom.xml发现jstl包导错了(Maven项目,使用Maven的坐标导包),jstl和standard包都需要。

    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-->
    <!--<dependency>-->
      <!--<groupId>javax.servlet.jsp.jstl</groupId>-->
      <!--<artifactId>jstl</artifactId>-->                   以前的jstl包  发现会导致错误
      <!--<version>1.2</version>-->
    <!--</dependency>-->
     <!--https://mvnrepository.com/artifact/jstl/jstl     换成这个就对了 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/taglibs/standard -->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

错误二:el表达式${}标签的数据在页面没有显示


将上面的一个错误解决后,又遇见了一个新的BUG。一波未平,一波又起。开始怀疑人生了。。。但是生活还得继续,BUG也必须的逐一解决奋斗。在不断的百度,Google后,终于找到一个解决方案。在JSP页面的头部加入一个标签开启el功能。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%--加入该标签手动开启el功能--%>
<%@page isELIgnored="false"%>
原因:JSP和Servlet版本导致el功能默认关闭,加入<%@page isELIgnored="false"%>标签手动开启el功能。

最后数据完整的显示出来了。


       遇到问题不可怕,关键是沉着冷静,慢慢思考,仔细查阅资料,不要放弃。当通过自己的努力解决问题后,会特别开心(即使是小问题,也是在进步)。如果文中写的有错误,或者有问题,还请读者毫不吝啬的指出来,一起学习一起进步嘛。


       每日鸡汤:接受真实的人生,真实的自己;改变能改变的,接受不能改变的。

Over!

猜你喜欢

转载自blog.csdn.net/chianz632/article/details/80890529