2

我正在尝试将 bean 对象的 Arraylist 显示到 HTML 表(JSP 页面)中。请帮助我哪里出错了。这是创建具有数据库数据的 bean 对象的代码。最后,所有 bean 对象都被添加到一个数组列表中。使用 RequestDispatcher 我正在将 arraylist 发送到 ViewData.jsp 现在请告诉如何将 arraylist 数据解释到表中。

package com.dsr.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import com.dsr.beans.EmpBean;
import com.dsr.db.DB;
import javax.servlet.RequestDispatcher;
import java.sql.*;

/**
 * Servlet implementation class for Servlet: CheckConn
 *
 */
 public class CheckConn extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public CheckConn() {
        super();
    }       

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }   

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        ArrayList<EmpBean> ebList= new ArrayList<EmpBean>();
        System.out.println(request.getParameter("date"));
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;



        try 
        {    
            DB dao=new DB();
            conn=dao.getConnection();  
            stmt=conn.createStatement();    

            rs = stmt.executeQuery("exec select_details");
            while(rs.next()){
                EmpBean eb = new EmpBean();
                eb.setEmp_id(rs.getString("emp_id"));
                eb.setFirst_name(rs.getString("first_name"));
                eb.setLast_name(rs.getString("last_name"));
                ebList.add(eb);
            }
            System.out.println(ebList.size());
            for (int i=0; i<ebList.size(); i++){
                EmpBean eb1= ebList.get(i);
                System.out.println(eb1.getEmp_id());
                System.out.println(eb1.getFirst_name());
                System.out.println(eb1.getLast_name());
            }
//          stmt.executeQuery("commit");

        }
        catch(Exception e)
        {        
            e.printStackTrace();    
        } 
        finally
        {
            try {
                conn.close();
                stmt.close();
                } catch (SQLException e) {

                e.printStackTrace();
            }


        }
        request.setAttribute("mybean",ebList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("viewData.jsp");
        if (dispatcher != null){
        dispatcher.forward(request, response);
        }

    }               
}

现在告诉我如何将 arraylist 数据解释为 html 表。

4

1 回答 1

3

在页面中使用JSTL :JSP

例如:

<table .... >
    <c:forEach var="empBean" items="${requestScope['mybean']}">
        <tr>
            <td>
                <c:out value="${empBean.first_name}"/>
            </td>
        </tr>
    </c:forEach>
</table>

也可以看看:

于 2013-05-02T15:22:21.240 回答