Page HTML qui n'affiche pas les données récuperées par un servlet DAO
Bonjour,
Désolé si le titre peut paraître étrange, je n'avais pas le moindre idée de comment appeler le sujet. : /
J'ai donc crée un servlet qui doit récupérer des données dans ma base de donnée et les afficher dans une page html tout ça dans un " système " DAO.
Le soucis étant la page html n'affiche rien.
Je vous link mes codes :
En premier ma classe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
package com.supinfo.supcommerce.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="Intervention")
@NamedQueries({
@NamedQuery(name="deleteInterventionById", query="DELETE FROM Intervention p WHERE p.id = :id"),
@NamedQuery(name="findAllInterventions", query="SELECT p FROM Intervention p")
})
public class Intervention implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String subject;
private String campus;
private String datepicker1;
private String datepicker2;
private String description;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="INTERVENTION_FK")
public Long getID(){
return id;
}
public void setId(Long id){
this.id=id;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject){
this.subject=subject;
}
public String getCampus()
{
return campus;
}
public void setCampus(String campus){
this.campus=campus;
}
public String getDatepicker1()
{
return datepicker1;
}
public void setDatepicker1(String datepicker1){
this.datepicker1=datepicker1;
}
public String getDatepicker2()
{
return datepicker2;
}
public void setDatepicker2(String datepicker2){
this.datepicker2=datepicker2;
}
public String getDescription()
{
return description;
}
public void setDescription(String description){
this.description=description;
}
} |
La classe DAO
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package com.supinfo.supcommerce.dao.jpa;
import java.util.*;
import javax.persistence.*;
import com.supinfo.supcommerce.dao.InterventionDAO;
import com.supinfo.supcommerce.entity.Intervention;
public class JpaInterventionDAO implements InterventionDAO {
private EntityManagerFactory emf;
public JpaInterventionDAO(EntityManagerFactory emf)
{
this.emf =emf;
}
@Override
public void createIntervention(Intervention p)
{
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try {
em.persist(p);
tr.commit();
} finally {
if(tr.isActive())
tr.rollback();
em.close();
}
}
@Override
public Intervention findIntervention(Long id)
{
EntityManager em = emf.createEntityManager();
Intervention p = null;
try{
p = em.find(Intervention.class ,id);
}finally {
em.close();
}
return p;
}
@Override
public void updateIntervention(Intervention p)
{
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try{
em.merge(p);
tr.commit();
} finally {
if(tr.isActive())
tr.rollback();
em.close();
}
}
@Override
public void deleteIntervention(Intervention p){
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try{
em.merge(p);
tr.commit();
} finally {
if(tr.isActive())
tr.rollback();
em.close();
}
}
@Override
public void deleteIntervention(Long id)
{
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try{
Query q = em.createNamedQuery("deleteProductById");
q.setParameter("id", id);
q.executeUpdate();
tr.commit();
} finally {
if(tr.isActive())
tr.rollback();
em.close();
}
}
@SuppressWarnings("unchecked")
@Override
public List<Intervention> findAllInterventions() {
List<Intervention> interventions;
EntityManager em = emf.createEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try{
Query q = em.createNamedQuery("findAllInterventions");
interventions = q.getResultList();
tr.commit();
} finally {
if(tr.isActive())
tr.rollback();
em.close();
}
return interventions;
}
} |
Interface DAO :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
package com.supinfo.supcommerce.dao;
import java.util.*;
import com.supinfo.supcommerce.entity.Intervention;
public interface InterventionDAO {
public void createIntervention(Intervention p);
public Intervention findIntervention(Long id);
public void updateIntervention(Intervention p);
public void deleteIntervention(Intervention p );
public void deleteIntervention(Long id);
public List<Intervention> findAllInterventions();
} |
Le fameux servlet pour envoyer les données vers la page html :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package com.supinfo.supcommerce.servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.supinfo.supcommerce.dao.DAOFactory;
import com.supinfo.supcommerce.entity.Intervention;
@WebServlet(urlPatterns={"/interventionMarseille"})
public class InterventionMarseille extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
List<Intervention> interventions = DAOFactory.getInterventionDAO().findAllInterventions();
request.setAttribute("interventions", interventions);
RequestDispatcher rd = request.getRequestDispatcher("/interventionMarseille.jsp");
rd.forward(request,response);
}
} |
Et pour finir la page html en elle-même ( qui est devenu un peu bordélique pour essayer : / ) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.supinfo.supcommerce.dao.InterventionDAO" %>
<%@page import="java.util.List" %>
<%@page import="com.supinfo.supcommerce.entity.Intervention" %>
<%@page import="com.supinfo.supcommerce.dao.jpa.JpaInterventionDAO" %>
<html>
<head>
<meta http-equiv="Content-Type" >
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<p>${interventions.datepicker}</p>
<p>${p.DATEPICKER1}</p>
<p>${interventions.SUBJECT}</p>
<table>
<tr>
<th>${p.datepicker}</th>
<th>${p.DATEPICKER1}</th>
<th>${interventions.DATEPICKER1}</th>
<th>${interventions.SUBJECT}</th>
<td>${interventions.subject}</td><td>${interventions.datepicker1}</td>
</tr>
<c:forEach items="${interventions}" var="p" >
<tr>
<td>${p.subject}</td>
<td>${p.datepicker1}</td>
<td>${p.datepicker2}</td>
</tr>
</c:forEach>
</table>
</body>
</html> |
Ca fait beaucoup de code à regarder, j'en suis bien conscient.
Ce qui m'étonne c'est que tout me semble juste, le code se compile très bien,
peut-être une erreur au niveau de la namedquery qui est fausse, j'ai essayé pas mal de chose mais rien ne marche.
Quand j'utilise Jdbc pour afficher les données, cela marche très bien, donc l'erreur ne doit pas venir de la base de donnée.
J'espère avoir poster dans la bonne rubrique, n'ayant pas trouver de forum dao spécifiquement.
Voila voila , merci d'avance. ( toute idée est la bienvenue car la je sèche : / )