无法显示数据列表,没有报错

来源:2-5 数据展示

幕布斯9147654

2016-09-24 10:38

package com.imooc.servlet;


import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.bean.Message;

/**

 * 列表页面初始化控制

 * @author Administrator

 *

 */

public class ListServlet extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

doGet(req, resp);

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);

try {

Class.forName("com.mysql.jdbc.Driver");

try {

Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/micro_message","root","123456");

String sql = "select ID,COMMAND,DESCRIPTION,CONTENT from message";

PreparedStatement ps = conn.prepareStatement(sql);

ResultSet rs =  ps.executeQuery();

List<Message> messageList = new ArrayList<Message>();

while(rs.next()){

Message message = new Message();

message.setId(rs.getString("ID"));

message.setCommand(rs.getString("COMMAND"));

message.setContent(rs.getString("CONTENT"));

message.setDescription(rs.getString("DESCRIPTION"));

System.out.println(message);

messageList.add(message);

}

req.setAttribute("messageList", messageList);

} catch (SQLException e) {

e.printStackTrace();

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}




<tbody>

<tr>

   <th><input type="checkbox" id="all" onclick=""/></th>

   <th>序号</th>

   <th>指令名称</th>

   <th>描述</th>

   <th>操作</th>

</tr style="background-color:#ECF6EE;">

<c:forEach items="${messageList}" var="message" varStatus="status">

<tr>

<td><input type="checkbox" /></td>

<td>${status.index+1}</td>

<td>${message.command}</td>

<td>${message.description}</td>

<td>

<a href="#">修改</a>&nbsp;&nbsp;&nbsp;

<a href="#">删除</a>

</td>

</tr>

</c:forEach>

</tbody>


写回答 关注

1回答

  • nuli5
    2016-09-24 20:05:48

    应该把req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp); 放在方法最后。

    代码是从上到下依次执行的

    哎呀呀125

    解决了我的问题,非常感谢!

    2017-01-19 16:59:51

    共 1 条回复 >

通过自动回复机器人学Mybatis---基础版

微信公众号自动回复功能学习Mybatis,基础教程加案例实战方式学习

107412 学习 · 786 问题

查看课程

相似问题