Olá, pessoal do GUJ!
Tudo bem?
Estava iniciando meus estudos em JSP, e eis que me surgiu uma dúvida.
Dêem uma olhada:
- Criei uma Servlet. Ela irá se comunicar com um DAO para requerer dados. Por hora, estou fazendo os dados na mão só para teste.
private List<Noticias> noticiasList;
private RequestDispatcher requestDispatcher;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.noticiasList = new ArrayList<Noticias>();
this.noticiasList.add(new Noticias(new Long(1), "Esporte", "Notícia 1."));
this.noticiasList.add(new Noticias(new Long(2), "Esporte", "Notícia 2."));
this.noticiasList.add(new Noticias(new Long(3), "Televisão", "Notícia 3."));
this.noticiasList.add(new Noticias(new Long(4), "Televisão", "Notícia 4."));
this.noticiasList.add(new Noticias(new Long(5), "Trânsito", "Notícia 5."));
this.noticiasList.add(new Noticias(new Long(6), "Trânsito", "Notícia 6."));
this.noticiasList.add(new Noticias(new Long(7), "Cinema", "Notícia 7."));
this.noticiasList.add(new Noticias(new Long(8), "Cinema", "Notícia 8."));
this.noticiasList.add(new Noticias(new Long(9), "Saúde", "Notícia 9."));
this.noticiasList.add(new Noticias(new Long(10), "Saúde", "Notícia 10."));
request.setAttribute("noticias", this.noticiasList);
this.requestDispatcher = request.getRequestDispatcher("/Noticias.jsp");
this.requestDispatcher.forward(request, response);
}
- Criei uma página JSP, a qual receberá esses dados que gerei na noticiasList.
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>.: Últimas Notícias - Notícias em Tempo Real :.</title>
</head>
<body>
<h1>Últimas Notícias:</h1>
<c:if test="${!empty noticias}">
<c:forEach var="noticia" items="${noticias}">
<p>${noticia.titulo}</p>
<p>${noticia.noticia}</p>
</c:forEach>
</c:if>
<c:if test="${empty noticias}">
Não há nenhuma notícia a ser exibida.
</c:if>
</body>
</html>
Mesmo eu preenchendo a lista dentro da minha Servlet, sempre cai no if dizendo que não há notícias!
Creio que seja um erro bobo!
Gostaria que, se possível, me ajudassem a corrigir este problema.
Desde já agradecido, fico no aguardo!
Abraço a todos!