IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JSF Java Discussion :

Alimenter une liste déroulante


Sujet :

JSF Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mai 2010
    Messages
    29
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 29
    Par défaut Alimenter une liste déroulante
    Bonjour,

    J'ai besoin d'alimenter une liste déroulante depuis ma base de données.

    J'ai un formulaire pour l'ajout des Produits (Nom, Firme, Type) et dans ce formulaire je dois sélectionner un type parmi les types saisis dans la base de données, donc je dois avoir une liste déroulante des différents types de la base.

    Base de données Mysql: 2 table: -Type(id , intitule).
    - Produit( id, nom, Firme, idType).

    J'utilise Hibernate 3 et JSF (myfaces).

    Comment dois-je m'y prendre ?

    Merci d'avance pour votre aide.

  2. #2
    Membre averti
    Inscrit en
    Mai 2010
    Messages
    29
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 29
    Par défaut Alimenter une liste déroulante
    voila ma page Formulaire.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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <%@ page contentType="text/html" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Formulaire</title>
    </head>
     
    <body>
    <f:view>
     	  <h:form>
     
    	  <table width="95%" border="0">  
            <tr>
              <td>nom </td>
              <td><h:inputText value="#{beanProduit.nomProduit}" required="true" id="aunInscription">
                  <f:validateLength minimum="9" maximum="9" />
                </h:inputText>          </td>
     
            </tr>
            <tr>
              <td>firme</td>
              <td><h:inputText value="#{beanProduit.firmeProduit}" required="true" id="nomEtudiant">
                  <f:validateLength minimum="3" maximum="20" />
                </h:inputText>          </td>
     
            </tr>
     
           <tr>
              <td>Type</td>
              <td><h:selectOneMenu value="#{beanProduit.idType}">
     
                </h:selectOneMenu>          </td>
              <td class="erreur"></td>
            </tr>
     
            <tr>
              <td width="105" class="seConnecter">
    		  <h:commandLink value="Ajouter" action="#{beanProduit.addProduit}"/></td>
              <td width="109" class="seConnecter">
    		  <h:commandLink value="Annuler" action="cancel" /></td>
     
            </tr>
          </table>
       </h:form>
     
    </f:view>
    </body>
    </html>
    mon bean Produit:
    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
     
    public class beanProduit{
    	private String nomProduit;
    	private String firmeProduit;
    	private String idType;
    	private List<SelectItem> listTypes;
     
     
    	public List<SelectItem> getlistTypes()
        {
            List<SelectItem> typeliste = new ArrayList <SelectItem>();
     
                Session session=HibernateUtil.getSessionFactory().openSession();
                List result = session.createQuery("from type").list(); 
                      Iterator it = result.iterator();
                while (it.hasNext()){
                            Type elementCourant = (Type)it.next();
                            typeliste.add(new SelectItem(elementCourant.getId().toString(),elementCourant.getIntitule()));
     
                }
            return typeliste;
        }
     
    	public String addProduit()
    	{
    		try{
     
    		package.produit.addProduit(Integer.valueOf(idType), nomProduit, firmeProduit);
     
    		return "ajoutReussi";
    		}
    		catch (Exception exp){
    			System.out.println("ajout non réussi");
    			return "ajoutNOReussi";}
     
    	}
     
    	public void setProduit(String nomProduit,String firmeProduit, String idFiliere){
    		this.nomProduit=nomProduit;
    		this.firmeProduit=firmeProduit;
    		this.idType=idType;
     
    	}
     
    //getters/setters des attributs.}
    voila mon bean Type:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public class beanType {
     
            String intitule;
       public String addType(){}
       public void setType(String intitule){}
      getter/setter(intitule)
      }
    et j'ai ajouter les fichier hbm(Produit.hbm.xml et Type.hbm.xml), et j'ai aussi les fichier généré par hibrnate(DAO, DAO.base).
    Merci a ceux qui vont m'aider.

  3. #3
    Membre averti
    Inscrit en
    Mai 2010
    Messages
    29
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 29
    Par défaut jsf liste déoulante
    personne?????????????????

  4. #4
    Membre éclairé
    Inscrit en
    Février 2008
    Messages
    64
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 64
    Par défaut
    Citation Envoyé par amattoallah Voir le message
    voila ma page Formulaire.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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <%@ page contentType="text/html" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Formulaire</title>
    </head>
     
    <body>
    <f:view>
     	  <h:form>
     
    	  <table width="95%" border="0">  
            <tr>
              <td>nom </td>
              <td><h:inputText value="#{beanProduit.nomProduit}" required="true" id="aunInscription">
                  <f:validateLength minimum="9" maximum="9" />
                </h:inputText>          </td>
     
            </tr>
            <tr>
              <td>firme</td>
              <td><h:inputText value="#{beanProduit.firmeProduit}" required="true" id="nomEtudiant">
                  <f:validateLength minimum="3" maximum="20" />
                </h:inputText>          </td>
     
            </tr>
     
           <tr>
              <td>Type</td>
              <td><h:selectOneMenu value="#{beanProduit.idType}">
     
                </h:selectOneMenu>          </td>
              <td class="erreur"></td>
            </tr>
     
            <tr>
              <td width="105" class="seConnecter">
    		  <h:commandLink value="Ajouter" action="#{beanProduit.addProduit}"/></td>
              <td width="109" class="seConnecter">
    		  <h:commandLink value="Annuler" action="cancel" /></td>
     
            </tr>
          </table>
       </h:form>
     
    </f:view>
    </body>
    </html>
    mon bean Produit:
    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
     
    public class beanProduit{
    	private String nomProduit;
    	private String firmeProduit;
    	private String idType;
    	private List<SelectItem> listTypes;
     
     
    	public List<SelectItem> getlistTypes()
        {
            List<SelectItem> typeliste = new ArrayList <SelectItem>();
     
                Session session=HibernateUtil.getSessionFactory().openSession();
                List result = session.createQuery("from type").list(); 
                      Iterator it = result.iterator();
                while (it.hasNext()){
                            Type elementCourant = (Type)it.next();
                            typeliste.add(new SelectItem(elementCourant.getId().toString(),elementCourant.getIntitule()));
     
                }
            return typeliste;
        }
     
    	public String addProduit()
    	{
    		try{
     
    		package.produit.addProduit(Integer.valueOf(idType), nomProduit, firmeProduit);
     
    		return "ajoutReussi";
    		}
    		catch (Exception exp){
    			System.out.println("ajout non réussi");
    			return "ajoutNOReussi";}
     
    	}
     
    	public void setProduit(String nomProduit,String firmeProduit, String idFiliere){
    		this.nomProduit=nomProduit;
    		this.firmeProduit=firmeProduit;
    		this.idType=idType;
     
    	}
     
    //getters/setters des attributs.}
    voila mon bean Type:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public class beanType {
     
            String intitule;
       public String addType(){}
       public void setType(String intitule){}
      getter/setter(intitule)
      }
    et j'ai ajouter les fichier hbm(Produit.hbm.xml et Type.hbm.xml), et j'ai aussi les fichier généré par hibrnate(DAO, DAO.base).
    Merci a ceux qui vont m'aider.
    j'ai essayé de te corriger un petit le code donc que j'ai ajouté:
    -dans la balise selectOneMenu il faut ajouter un composant selectItems en lui donnant comme value la méthode getListTypes();
    -la méthode getListTypes() il faut qu'elle respecte la norme des javaBean le get+première lettre en majuscule.

    Bonne chance

    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
    <%@ page contentType="text/html" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Formulaire</title>
    </head>
     
    <body>
    <f:view>
     	  <h:form>
     
    	  <table width="95%" border="0">  
            <tr>
              <td>nom </td>
              <td><h:inputText value="#{beanProduit.nomProduit}" required="true" id="aunInscription">
                  <f:validateLength minimum="9" maximum="9" />
                </h:inputText>          </td>
     
            </tr>
            <tr>
              <td>firme</td>
              <td><h:inputText value="#{beanProduit.firmeProduit}" required="true" id="nomEtudiant">
                  <f:validateLength minimum="3" maximum="20" />
                </h:inputText>          </td>
     
            </tr>
     
           <tr>
              <td>Type</td>
              <td><h:selectOneMenu value="#{beanProduit.idType}">
                 <f:selectItems value="#{beanProduit.listTypes}"/>
                </h:selectOneMenu>          </td>
              <td class="erreur"></td>
            </tr>
     
            <tr>
              <td width="105" class="seConnecter">
    		  <h:commandLink value="Ajouter" action="#{beanProduit.addProduit}"/></td>
              <td width="109" class="seConnecter">
    		  <h:commandLink value="Annuler" action="cancel" /></td>
     
            </tr>
          </table>
       </h:form>
     
    </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
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    public class beanProduit{
    	private String nomProduit;
    	private String firmeProduit;
    	private String idType;
    	private List<SelectItem> listTypes;
     
     
    	public List<SelectItem> getListTypes()
        {
            List<SelectItem> typeliste = new ArrayList <SelectItem>();
     
                Session session=HibernateUtil.getSessionFactory().openSession();
                List result = session.createQuery("from type").list(); 
                      Iterator it = result.iterator();
                while (it.hasNext()){
                            Type elementCourant = (Type)it.next();
                            typeliste.add(new SelectItem(elementCourant.getId().toString(),elementCourant.getIntitule()));
     
                }
            return typeliste;
        }
     
    	public String addProduit()
    	{
    		try{
     
    		package.produit.addProduit(Integer.valueOf(idType), nomProduit, firmeProduit);
     
    		return "ajoutReussi";
    		}
    		catch (Exception exp){
    			System.out.println("ajout non réussi");
    			return "ajoutNOReussi";}
     
    	}
     
    	public void setProduit(String nomProduit,String firmeProduit, String idFiliere){
    		this.nomProduit=nomProduit;
    		this.firmeProduit=firmeProduit;
    		this.idType=idType;
     
    	}
     
    //getters/setters des attributs.}

  5. #5
    Membre averti
    Inscrit en
    Mai 2010
    Messages
    29
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 29
    Par défaut jsf hibernate liste déroulante
    ça marche pas!!!!!!!!!!!!!!
    Il m'affiche une liste vide comme s'il y a aucune connexion à la base.

Discussions similaires

  1. Réponses: 22
    Dernier message: 18/06/2008, 19h01
  2. [ODBC] Alimenter une liste déroulante par un lien ODBC
    Par Mut dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 27/08/2007, 15h06
  3. [MySQL] alimenter une liste déroulante via une requête
    Par stefon dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 13/12/2006, 13h32
  4. Réponses: 15
    Dernier message: 21/11/2006, 10h13
  5. alimenter une liste déroulante
    Par wided_instm dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 15/09/2006, 19h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo