将结果集保存为arraylist

时间:2017-04-21 01:28:09

标签: sql jsp tomcat java-ee jstl

我无法完成正在制作的网络应用的功能。

以下代码显然没有产生任何错误,

    //This is the trouver ( find ) function, code is related to DAO pattern.
    public ArrayList<Ticket> trouver( int id_employe ) throws DAOException {
    ArrayList<Ticket> liste = new ArrayList<Ticket>();
    Connection connexion = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Ticket ticket = null;

    try {
        connexion = daoFactory.getConnection();
        preparedStatement = initialisationRequetePreparee( connexion, SQL_SELECT_PAR_EMP, false, id_employe );
        resultSet = preparedStatement.executeQuery();

        if ( resultSet.next() ) {
            ticket = map( resultSet );//map function stores a row's result in a Bean.
            liste.add(ticket);
        }
    } catch ( SQLException e ) {
        throw new DAOException( e );
    } finally {
        fermeturesSilencieuses( resultSet, preparedStatement, connexion );
    }
    return liste;
}

通过servlet:

ArrayList<Ticket> lticket = ticketform.recupererTicket(request);
request.setAttribute("lticket", lticket);
this.getServletContext().getRequestDispatcher(VUE).forward(request, response);

错误开始出现在视图中:

org.apache.jasper.JasperException: An exception occurred processing JSP page /ListeTickets.jsp at line 29
26: </tr>
27: <c:forEach items="${ requestScope.lticket }" var="mapticket">
28: <tr>
29: <c:forEach items="${mapticket}" var="ticket">
30: <td><c:out value="${ ticket.sujet }"/></td>
31: <td><c:out value="${ ticket.description }"/></td>
32: <td><c:out value="${ ticket.priorite }"/></td>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)

长话短说,我有一个存储在ArrayList中的Beans列表,需要两个循环来迭代整个数据,但是出现了错误。

1 个答案:

答案 0 :(得分:3)

根据您的代码,它应该只有一个<c:forEach items="${ lticket }" var="ticket"> <tr> <td><c:out value="${ ticket.sujet }"/></td> <td><c:out value="${ ticket.description }"/></td> <td><c:out value="${ ticket.priorite }"/></td> </tr> </c:forEach>

{{1}}