Bonjour à tous,
Je viens d'afficher ma premiere page Jsf. Mais pas totalement. J'ai beaucoup ramé pour le déploiement. Ainsi j'ai opté pour l'environnement qui me semble le plus facile à intégrer (NetBeans+GlassFissh+TopLink). Passons.
Mon problème est que je n'arrive pas à afficher ma dataTable. Elle doit itérer sur une liste de personnes. J'ai joints des copies de l'arborescence du projet Personne.
Voici mes sources:
Entité Personne.java
Interface locale PersonneLocale
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; /** * * @author */ @Entity @Table(name = "personne") @NamedQueries({@NamedQuery(name = "Personne.findByIdPersonne", query = "SELECT p FROM Personne p WHERE p.idPersonne = :idPersonne"), @NamedQuery(name = "Personne.findByNom", query = "SELECT p FROM Personne p WHERE p.nom = :nom"), @NamedQuery(name = "Personne.findByPrenom", query = "SELECT p FROM Personne p WHERE p.prenom = :prenom")}) public class Personne implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "idPersonne", nullable = false) private Integer idPersonne; @Column(name = "nom", nullable = false) private String nom; @Column(name = "prenom", nullable = false) private String prenom; public Personne() { } public Personne(Integer idPersonne) { this.idPersonne = idPersonne; } public Personne(Integer idPersonne, String nom, String prenom) { this.idPersonne = idPersonne; this.nom = nom; this.prenom = prenom; } public Integer getIdPersonne() { return idPersonne; } public void setIdPersonne(Integer idPersonne) { this.idPersonne = idPersonne; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } @Override public int hashCode() { int hash = 0; hash += (idPersonne != null ? idPersonne.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 Personne)) { return false; } Personne other = (Personne) object; if ((this.idPersonne == null && other.idPersonne != null) || (this.idPersonne != null && !this.idPersonne.equals(other.idPersonne))) { return false; } return true; } @Override public String toString() { return "entity.Personne[idPersonne=" + idPersonne + "]"; } }
Classe EJB Stateless PersonneBeanDao
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package stateless; import entity.Personne; import java.util.List; import javax.ejb.Local; /** * * @author */ @Local public interface PersonneLocale { public Personne CreatePersonne(Personne pers); public List<Personne> FindAllPersonne(); public List<Personne> getPersonne(); }
Contrôleur Jsf PersonneController
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package stateless; import entity.Personne; import java.util.List; import javax.persistence.EntityManager; //import javax.persistence.PersistenceContext; import javax.persistence.Persistence; /** * * @author */ public class PersonneBeanDao implements PersonneLocale{ //@PersistenceContext(unitName="PersonnePU") private EntityManager em; private static final String JPA_UNIT_NAME="PersonnePU2"; protected EntityManager getEntityManager() { if (em==null) em=Persistence.createEntityManagerFactory(JPA_UNIT_NAME).createEntityManager(); return em; } public Personne CreatePersonne(Personne pers) { throw new UnsupportedOperationException("Not supported yet."); } public List<Personne> FindAllPersonne() { List<Personne> persons = getEntityManager().createQuery("Select p from personne p").getResultList(); return persons; } public List<Personne> getPersonne() { throw new UnsupportedOperationException("Not supported yet."); } }
index.jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Jsf; import entity.Personne; import java.util.List; import stateless.PersonneBeanDao; /** * * @author */ public class PersonneController { private PersonneBeanDao persDAO =new PersonneBeanDao(); private List<Personne> personnes; public List<Personne> getPersonne(){ if(personnes==null) personnes=persDAO.FindAllPersonne(); return personnes; } /* public List<Personne> doFindAllPersonne(){ if(persons==null) persons=persDAO.FindAllPersonne(); return persons; }*/ }
list.jsp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <%-- Document : index Created on : 19 août 2008, 12:25:45 Author : --%> <%@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>JSP Page</title> </head> <body> <jsp:forward page="list.jsf"/> </body> </html>
web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <%-- Document : list Created on : 19 août 2008, 14:35:24 Author : --%> <%@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"> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Liste des personnes</title> </head> <body> <f:view> <h1><h:outputText value="Hello avec JSF" /></h1> <h:form> <h:outputText value="Prénom" /> <h:inputText /><br> <h:outputText value="Nom" /> <h:inputText /><br> <h:commandButton value="Envoyer" /> </h:form> <h:dataTable border="0" rules="all" value="#{personneCtrl.personne}" var="p"> <h:column> <f:facet name="header"> <h:outputText value="Prénom" /> </f:facet> <h:outputText value="#{p.prenom}" /> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Nom" /> </f:facet> <h:outputText value="#{p.nom}" /> </h:column> </h:dataTable> </f:view> </body> </html>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Personne</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
et enfin faces-config.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="UTF-8"?> <!-- Document : faces-config.xml Created on : 19 août 2008, 12:40 Author : Description: Purpose of the document follows. --> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version="1.2"> <managed-bean> <managed-bean-name>personneCtrl</managed-bean-name> <managed-bean-class>Jsf.PersonneController</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>login</managed-bean-name> <managed-bean-class>entity.LoginBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>
Partager