Un grand bonjour aux développeurs de developpez
D'abord merci pour ce formidable site.
J'ai un problème concernant un formulaire JSF et je n'arrive pas à m'en sortir.
Je travaille une application avec JSF 2.0 / Hibernate. Le tout sous Netbeans 6.8
Je suis parvenue à faire de l'ajout et de la suppression mais je n'arrive pas à faire de la recherche.
Voilà ce que je veux faire :
J'aimerais qu'à partir d'un formulaire de recherche (un seul input de l'id) parvenir à afficher l'élément recherché dans une dataTable.
Voilà ce que j'ai fait :
1- Ma classe Authentification.java générée à partir du mapping Hibernate:
________________________________________________________________
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 public class Authentification implements java.io.Serializable { private int idauth; private String loginauth; private String passauth; public Authentification() { } public Authentification(int idauth) { this.idauth = idauth; } public Authentification(int idauth, String loginauth, String passauth) { this.idauth = idauth; this.loginauth = loginauth; this.passauth = passauth; } public int getIdauth() { return this.idauth; } public void setIdauth(int idauth) { this.idauth = idauth; } public String getLoginauth() { return this.loginauth; } public void setLoginauth(String loginauth) { this.loginauth = loginauth; } public String getPassauth() { return this.passauth; } public void setPassauth(String passauth) { this.passauth = passauth; } }
2- Ma classe "AuthHelper.java" qui contient les fonctions manipulant les requêtes HQL :
_________________________________________________________________
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 public class authHelper { Session session = null; List<Authentification> authList = null; public authHelper(){ this.session = HibernateUtil.getSessionFactory().getCurrentSession(); } public List getAuthByID(){ Authentification auth = null; try { org.hibernate.Transaction tx = session.beginTransaction(); Query q = session.createQuery("from Authentification as au where au.idauth=" + auth.getIdauth()); auth = (Authentification) q.uniqueResult(); } catch (Exception e) { e.printStackTrace(); } return authList; } }
3- Ma classe "SearchBean.java" qui représente le ManagedBean :
_________________________________________________________________
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 @ManagedBean(name="SearchBean") @SessionScoped public class SearchBean { private DataModel authentication; authHelper helper = new authHelper(); private Authentification auth = new Authentification (); List<Authentification> authList; /** Creates a new instance of SearchBean */ public SearchBean() { } public Authentification getAuth(){return auth;} public void setAuth(Authentification auth){this.auth=auth;} public DataModel getAuthentif() { if (authentication == null) { authentication = new ListDataModel(helper.getAuthByID()); } return authentication; } public String rechAuth(){ helper.getAuthByID(); //auth=new Authentification(); authList=helper.getAuthByID(); //authModel.setWrappedData(helper.getAuthentication()); return "trouve"; } void recreateModel() { authentication = null; } }
4- Mon index.xhtml qui contient le formulaire de recherche (recherche par ID) :
_________________________________________________________________
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <center><h:form id="helloForm"> <h:panelGrid columns="3"> <h:outputText value="id"/> <h:inputText id="id" value="#{SearchBean.auth.idauth}" required="true"/> <h:message for="id" styleClass="error"/> <h:commandButton action="#{SearchBean.rechAuth}" value="Rechercher"/> </h:panelGrid> </h:form></center>
5- Si je clique sur le bouton rechercher j'obtiens la page trouve.xhtml contenant un dataTable contenant le résultat de ma requête :
_________________________________________________________________
Code xml : 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 <h:form> <h:dataTable id="authentication" value="#{SearchBean.getAuthentif()}" var="item" border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px"> <h:column> <f:facet name="header"> <h:outputText value="idauth"/> </f:facet> <h:outputText value="#{item.idauth}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="login"/> </f:facet> <h:outputText value="#{item.loginauth}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText value="mot de passe"/> </f:facet> <h:outputText value="#{item.passauth}"/> </h:column> </h:dataTable> <br/> </h:form>
Aidez moi svp je suis débutante et je n'arrive pas encore à régler ce problème merci
Partager