Java/JSTL - Table listing with c:foreach

Asked

Viewed 4,499 times

0

I tried to list database information on a page JSP by means of the tag <c:forEach> but I’m not getting any return value.

Bookoid:

    @Override
public List<Livro> consultar() throws SQLException{

            List<Livro> livros = new ArrayList<Livro>();
            try {
            String sql = "SELECT * FROM liros WHERE st_ativo=1";
            Statement stmt = con.createStatement();
            ResultSet result = stmt.executeQuery(sql);

            while(result.next()) {
                Livro li = new Livro();
                li.setTitulo(result.getString("titulo"));
                li.setPaginas(result.getInt("paginas"));
                li.setAutor(result.getString("autor"));
                li.setEditora(result.getString("editora"));
                livros.add(li);
            }

        } catch(SQLException e) {
            e.printStackTrace();
        }
        return livros;
}

Listbook.jsp:

    <tr>
    <th>Titulo</th>
    <th>Páginas</th>
    <th>Autor</th>
    <th>Editora</th>
    <th>Editar</th>
    <th>Deletar</th>
</tr>
</thead>
<tbody>
<c:forEach var="li" items="$(livros)">
        <tr>
            <td><c:out value="$(li.titulo)" /></td>
            <td><c:out value="$(li.paginas)"/></td>
            <td><c:out value="$(li.autor)"/></td>
            <td><c:out value="$(li.editora)"/></td>
            <td><a href="Servlet?action=editar&id" >Editar</a></td>
            <td><a href="Servlet?action=deletar&id" >Deletar</a></td>
        </tr>
</c:forEach>
</tbody>

Resposta que tenho quando executo o código

Note: I use the driver jstl1.2

What could be wrong with my code?

  • Utilize ${...} instead of $(...). That is, use keys, not parentheses.

1 answer

1


<c:forEach var="li" items="${livros}">
        <tr>
            <td><c:out value="${li.titulo}" /></td>
            <td><c:out value="${li.paginas}"/></td>
            <td><c:out value="${li.autor}"/></td>
            <td><c:out value="${li.editora}"/></td>
            <td><a href="Servlet?action=editar&id" >Editar</a></td>
            <td><a href="Servlet?action=deletar&id" >Deletar</a></td>
        </tr>
</c:forEach>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.