这个是在修改jsp代码时出现的,在此写文章,是为了记下自己反对错,说不定也可以帮助大家。
首先,上servlet代码:
@WebServlet("/detail") public class SelectServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Student> studentList = StudentUtils.getMySQL(); req.setAttribute("studentList", studentList); req.getRequestDispatcher("detail.jsp").forward(req, resp); } }
再是detail.jsp的代码:
<%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% List list = (List) request.getAttribute("studentList"); System.out.println("************"); System.out.println("list:" + list); System.out.println("************"); %> <html> <head> <title>慕课网用户管理中心</title> <link rel="stylesheet" href="lib/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/2.2.4/jquery-1.12.4.min.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="page-header"> <h1>慕课网后台管理系统 <small>用户数据管理中心</small> </h1> </div> </div> <div class="row"> <div class="jumbotron"> <h1>MyBatis基础入门课程!</h1> <p>通过一个项目来完成基础部分的学习</p> <p><a class="btn btn-primary btn-lg" href="#" role="button">查看更多,请上慕课网</a></p> <p><a class="btn btn-primary btn-lg" href="${pageContext.request.contextPath}/addstudents.jsp" role="button">新增用户</a> </p> </div> </div> <div class="row"> <table class="table table-hover table-striped"> <tr> <th>用户编号</th> <th>学号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>年级</th> <th>专业</th> </tr> <c:set var="student" value="${studentList}"></c:set> <%--<c:forEach var="student" items="${studentList}">--%> <tr> <td>${student.id}</td> <td>${student.reg_no}</td> <td>${student.name}</td> <td>${student.age}</td> <td>${student.sex}</td> <td>${student.grade}</td> <td>${student.major}</td> </tr> <%--</c:forEach>--%> </table> </div> </div> </body> </html>
没错,一旦你运行,就会报这个错误java.lang.NumberFormatException: For input string: “id”。
com.imooc.com.imooc.servlet.SelectServlet.doPost(SelectServlet.java:25) com.imooc.com.imooc.servlet.SelectServlet.doGet(SelectServlet.java:18)
这是具体的提示,我反复查找。
最后,发现了问题,原来自己将jsp代码修改了,原来的c: forEach标签被自己注释掉了,
把注释取消,去掉
<c:set var="student" value="${studentList}"></c:set>
就没问题了。
怪不得一直报错呢!
可以考虑:是不是自己的代码写错了,我以为自己的代码没问题,自己之前更改过代码。