Importante

Você está vendo a versão anterior da nova experiência da Alura que estamos preparando para você. Em breve, ela ganha uma identidade visual novinha totalmente pensada em potencializar seus estudos!

1
resposta

(JSP - Servlet) Problema com carregamento de dados na tabela

Estou tentando carregar os dados na tabela assim que ela é carregada, porém os dados só carregam quando eu adiciono um novo registro. Como é um projeto pessoal eu estou fazendo tudo na mesma página "index.jsp".

Servlet

@WebServlet("/registrarAcao")
public class Acao extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private AcaoDAO acaoDAO = new AcaoDAO();

    public Acao() {
        super();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        try {
            RequestDispatcher view = req.getRequestDispatcher("/index.jsp");
            req.setAttribute("investimentos", acaoDAO.listar());
            view.forward(req, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        String codigo = req.getParameter("codigo");
        String dataString = req.getParameter("data");

        String quantidade = req.getParameter("quantidade");
        String valor = req.getParameter("valor");

        AcaoBean acao = new AcaoBean();
        acao.setCodigo(codigo);
        acao.setData(dataString);
        acao.setQuantidade(Double.parseDouble(quantidade));
        acao.setValor(Double.parseDouble(valor));

        acaoDAO.salvar(acao);

        try {
            RequestDispatcher view = req.getRequestDispatcher("/index.jsp");
            req.setAttribute("investimentos", acaoDAO.listar());
            view.forward(req, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro de Investimentos</title>
<link rel="stylesheet" href="css/bootstrap-theme.css">
<link rel="stylesheet" href="css/bootstrap.min.css">

</head>
<body class="container bg-light">

    <h1 class="text-center">Registro de Investimentos</h1>
    <div id="mensagemView"></div>

    <form class="form" action="registrarAcao" method="post">

        <div class="form-group">
            <label for="valor">Código do Investimento ou Ação</label> <input
                id="codigo" name="codigo" type="text" class="form-control" required
                autofocus />
        </div>

        <div class="form-group">
            <label for="data">Data</label> <input type="date" id="data"
                name="data" class="form-control" />
        </div>

        <div class="form-group">
            <label for="quantidade">Quantidade</label> <input type="number"
                min="1" step="1" id="quantidade" name="quantidade"
                class="form-control" value="1" required />
        </div>

        <div class="form-group">
            <label for="valor">Valor</label> <input id="valor" name="valor"
                type="number" class="form-control" min="0.01" step="0.01"
                value="0.0" required />
        </div>

        <button class="btn btn-dark" type="submit">Adicionar</button>
    </form>

    <br>
    <br>

    <table class="table table-striped table-bordered">
        <thead class="thead-dark text-center">
            <tr>
                <th scope="col">CÓDIGO</th>
                <th scope="col">DATA</th>
                <th scope="col">QUANTIDADE</th>
                <th scope="col">VALOR</th>
                <th scope="col">VOLUME</th>
            </tr>
        </thead>

        <tbody>
            <c:forEach items="${investimentos}" var="inv">
                <tr>

                    <td><c:out value="${inv.codigo}"></c:out></td>
                    <td><c:out value="${inv.data}"></c:out></td>
                    <td><c:out value="${inv.quantidade}"></c:out></td>
                    <td>R$<c:out value="${inv.valor}"></c:out></td>
                    <td>R$<c:out value="${inv.valor * inv.quantidade}"></c:out></td>
                </tr>
            </c:forEach>
        </tbody>

        <tfoot>
        </tfoot>
    </table>


</body>
</html>
1 resposta

Pelo que entendi ao acessar /registrarAcao no browser você quer bater no banco de dados e carregar as informações no /index.jsp

Olhando seu código me parece ok, será que não está retornando nada no seu acaoDAO.listar()?