Olá pessoal já pesquisei bastante sobre o <c:foreach> , mas sempre acho a mesma coisa usando o [u]jsp:useBean[/u]
mas no meu não consigo chamar nem encontrar os metodos da classe que passei no [u]jsp:useBean[/u]
Alguém pode dar uma olhada no código e me dizer qual o meu problema
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body style="color: midnightblue">
<center>
<h1>Bem vindo ao Sistema do Concurso</h1>
</center>
<hr>
<center>
<h3>Cadastro</h3>
</center>
<hr>
</br>
<form id="formulario" action="ServletCadastro">
Nome: <input type="text" name="nome" size="50"></br>
Cpf : <input type="text" name="cpf" size="12"></br>
Nota da prova didática: <input type="text" name="notad" size="3"></br>
Nota da prova de títulos: <input type="text" name="notat" size="3"></br>
<input type="submit" value="Inserir" name="Inserir" >
<input type="submit" value="Editar" name="Editar" >
<input type="submit" value="Excluir" name="Excluir">
</br>
</br>
<hr>
</br>
Pesquisar Candidato por nome:
<input type="text" name="nomeP" size="50"></br>
<input type="submit" value="Pesquisar" name="Pesquisar">
<jsp:useBean id="cand" class="util.CandidatoDAO"></jsp:useBean>
<table>
<c:forEach var="item" items="cand.listaAux" >
<tr>
<td><c:out value="${item.nome}"/></td>
<td><c:out value="${item.cpf}"/></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
[code]
package modelo;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
-
@author Monnalisa Christina
*/
@Entity
public class Candidato implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private String cpf;
private Integer notaDid;
private Integer notaTit;…
GETS E SETS
…
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}@Override
public boolean equals(Object object) {
// TODO: Warning - this method won’t work in the case the id fields are not set
if (!(object instanceof Candidato)) {
return false;
}
Candidato other = (Candidato) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}@Override
public String toString() {
return “modelo.Candidato[ id=” + id + " ]";
}
}[/code]
o SERVLET
Tenho que usar o servlet e fazer as chamadas dos inserir, editar e excluir assim pq esse programa eh apenas para estudo
package Servlet;
import dao.CandidatoJpaController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Candidato;
import util.EMF;
/**
*
* @author Monnalisa Christina
*/
public class ServletCadastro extends HttpServlet {
private Candidato candidato = new Candidato();
private List<Candidato> lista = new ArrayList<Candidato>();
private CandidatoJpaController dao = new CandidatoJpaController(EMF.getEntityManagerFactory());
private String CandidatoPesquisado;
/*
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nome = request.getParameter("nome");
String cpf = request.getParameter("cpf");
String notaDid = request.getParameter("notad");
String notaTit = request.getParameter("notat");
CandidatoPesquisado = request.getParameter("nomeP");
String butIns = request.getParameter("Inserir");
String butEd = request.getParameter("Editar");
String butExc = request.getParameter("Excluir");
String butPes = request.getParameter("Pesquisar");
if (!"Pesquisar".equals(butPes)) {
if ("".equals(nome) || "".equals(cpf)) {
request.getServletContext().getRequestDispatcher("/erroValidacao.jsp").forward(request, response);
}
if ("".equals(notaDid) || "".equals(notaTit)) {
request.getServletContext().getRequestDispatcher("/erroValidacao.jsp").forward(request, response);
}
} else if ("Pesquisar".equals(butPes)) {
setLista(new ArrayList<Candidato>());
for (Candidato can : dao.findCandidatoEntities()) {
if ((can.getNome().equals(CandidatoPesquisado))) {
lista.add(can);
request.getServletContext().getRequestDispatcher("/foiPesquisado.jsp").forward(request, response);
}
}
}
try {
Integer n1 = Integer.parseInt(notaDid);
Integer n2 = Integer.parseInt(notaTit);
if ("Inserir".equals(butIns)) {
getCandidato().setNome(nome);
getCandidato().setCpf(cpf);
getCandidato().setNotaDid(n1);
getCandidato().setNotaTit(n2);
dao.create(getCandidato());
pesquisar();
request.setAttribute("nome", nome);
request.setAttribute("cpf", cpf);
request.setAttribute("notad", n1);
request.setAttribute("notat", n2);
request.getServletContext().getRequestDispatcher("/CadastroSucesso.jsp").forward(request, response);
} else if ("Editar".equals(butEd)) {
getCandidato().setId(Long.MIN_VALUE);
getCandidato().setNome(nome);
getCandidato().setCpf(cpf);
getCandidato().setNotaDid(n1);
getCandidato().setNotaTit(n2);
dao.edit(getCandidato());
pesquisar();
request.setAttribute("nome", nome);
request.setAttribute("cpf", cpf);
request.setAttribute("notad", n1);
request.setAttribute("notat", n2);
request.getServletContext().getRequestDispatcher("/CadastroSucesso.jsp").forward(request, response);
} else if ("Excluir".equals(butExc)) {
getCandidato().setId(Long.MIN_VALUE);
getCandidato().setNome(nome);
getCandidato().setCpf(cpf);
getCandidato().setNotaDid(n1);
getCandidato().setNotaTit(n2);
dao.destroy(getCandidato().getId());
pesquisar();
request.getServletContext().getRequestDispatcher("/CadastroSucesso.jsp").forward(request, response);
}
} catch (Exception e) {
request.getServletContext().getRequestDispatcher("/erro.jsp").forward(request, response);
} finally {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
public int pesquisar() {
setLista(dao.findCandidatoEntities());
return getLista().size();
}
/**
* @return the candidato
*/
public Candidato getCandidato() {
return candidato;
}
/**
* @param candidato the candidato to set
*/
public void setCandidato(Candidato candidato) {
this.candidato = candidato;
}
/**
* @return the lista
*/
public List<Candidato> getLista() {
return lista;
}
/**
* @param lista the lista to set
*/
public void setLista(List<Candidato> lista) {
this.lista = lista;
}
/**
* @return the CandidatoPesquisado
*/
public String getCandidatoPesquisado() {
return CandidatoPesquisado;
}
/**
* @param CandidatoPesquisado the CandidatoPesquisado to set
*/
public void setCandidatoPesquisado(String CandidatoPesquisado) {
this.CandidatoPesquisado = CandidatoPesquisado;
}
}
Código de onde chamo o listAux() no <c:foreach>
[code]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package util;
import dao.CandidatoJpaController;
import java.util.ArrayList;
import java.util.List;
import modelo.Candidato;
/**
*
-
@author Monnalisa Christina
*/
public class CandidatoDAO {private Candidato candidato = new Candidato();
private List lista = new ArrayList();
private CandidatoJpaController dao = new CandidatoJpaController(EMF.getEntityManagerFactory());
private String CandidatoPesquisado;/**
-
@return the candidato
*/
public Candidato getCandidato() {
return candidato;
}
/**
-
@param candidato the candidato to set
*/
public void setCandidato(Candidato candidato) {
this.candidato = candidato;
}
/**
-
@return the lista
*/
public List getLista() {
return lista;
}
/**
-
@param lista the lista to set
*/
public void setLista(List lista) {
this.lista = lista;
}
/**
-
@return the CandidatoPesquisado
*/
public String getCandidatoPesquisado() {
return CandidatoPesquisado;
}
/**
-
@param CandidatoPesquisado the CandidatoPesquisado to set
*/
public void setCandidatoPesquisado(String CandidatoPesquisado) {
this.CandidatoPesquisado = CandidatoPesquisado;
}
public int pesquisar() {
setLista(dao.findCandidatoEntities());
return getLista().size();
}public void carregar(Long id) {
Candidato can = dao.findCandidato(id);
candidato.setNome(can.getNome());
candidato.setCpf(can.getCpf());
candidato.setNotaDid(can.getNotaDid());
candidato.setNotaTit(can.getNotaTit());
candidato.setId(can.getId());if (candidato == null) { candidato = new Candidato(); }}
public List listaAux(){
pesquisar(); List<Candidato> aux = getLista(); return aux;}
}[/code] -
@return the candidato
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import dao.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Candidato;
/**
*
* @author Monnalisa Christina
*/
public class CandidatoJpaController implements Serializable {
public CandidatoJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Candidato candidato) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(candidato);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Candidato candidato) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
candidato = em.merge(candidato);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Long id = candidato.getId();
if (findCandidato(id) == null) {
throw new NonexistentEntityException("The candidato with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Candidato candidato;
try {
candidato = em.getReference(Candidato.class, id);
candidato.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The candidato with id " + id + " no longer exists.", enfe);
}
em.remove(candidato);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Candidato> findCandidatoEntities() {
return findCandidatoEntities(true, -1, -1);
}
public List<Candidato> findCandidatoEntities(int maxResults, int firstResult) {
return findCandidatoEntities(false, maxResults, firstResult);
}
private List<Candidato> findCandidatoEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Candidato.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Candidato findCandidato(Long id) {
EntityManager em = getEntityManager();
try {
return em.find(Candidato.class, id);
} finally {
em.close();
}
}
public int getCandidatoCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Candidato> rt = cq.from(Candidato.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
public Candidato findCan(String nome){
EntityManager em = getEntityManager();
TypedQuery<Candidato> query;
query = em.createQuery("select can from Candidato can where can.nome=:nome" , Candidato.class);
query.setParameter("nome", nome);
try{
return query.getSingleResult();
}catch(NoResultException e){
return null;
}
}
}