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 :

Liste déroulante en JSF


Sujet :

JSF Java

  1. #1
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2009
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2009
    Messages : 19
    Par défaut Liste déroulante en JSF
    Bonjour,

    je suis débutant et je voudrais développer une appli avec Spring, Hibernate et JSF.

    Mon problème est que je veux utiliser une liste déroulante dans un JSF et j'ai toujours des messages d'erreur.

    Sources utilisés :
    -->index.jsf
    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
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>    
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <f:view>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link rel="stylesheet" type="text/css" href="../css/styles.css" />
    <title>MEDIAGEST - Gestion de médiathèque personnelle</title>
     
    </head>
    <body>
    <f:loadBundle var="msg" basename="fr.ducrocql.dao.message" />
    <div align="center">
    Bienvenue dans l'application MEDIAGEST.
    Gestion de médiathèque personnelle.
    <br />
     
    <h:form>
      <h:commandLink action="#{interpreteBean.gererInterprete}" value="#{msg.boutongestionInterp}" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <h:commandLink action="#{disqueBean.gererDisque}" value="Gérer disques">
      </h:commandLink>
      &nbsp;&nbsp;&nbsp;&nbsp;
      <h:commandLink action="#{cassetteBean.gererCassette}" value="Gérer cassettes"></h:commandLink>
      &nbsp;&nbsp;&nbsp;&nbsp;
      <h:commandLink action="#{livreBean.gererLivre}" value="Gérer livres"></h:commandLink>
      <br>
      <h:commandLink value="test" action="#{formatBean.gererFormat}" />
    </h:form>
     
    </div>
    </body>
    </html>
    </f:view>
    --> testList.jsf
    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
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>    
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     
    <f:view>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <link rel="stylesheet" type="text/css" href="../css/styles.css" />
    </head>
    <body>
    <h:form>
    	 	<h:outputLabel>Format</h:outputLabel>
    		<h:selectOneMenu value="#{formatBean.libelle}" id="chx">  
    		<f:selectItems value="#{formatBean.tousFormats}"/> 
    	</h:selectOneMenu>  
    	<h:commandButton value="ok" />
     
    </h:form>
    </body>
    </html>
    </f:view>
    --> FormatBean
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    /*
     * Created on 15 juin 2009
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package fr.ducrocql.beans;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
     
    import javax.faces.model.SelectItem;
     
    import fr.ducrocql.model.Format;
    import fr.ducrocql.services.FormatService;
    import fr.ducrocql.utils.Utils;
     
    /**
     * @author Laurent
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class FormatBean {
    	private FormatService formatService = (FormatService)Utils.getContext().getBean("formatService"); 
    	private List<SelectItem> tousFormats = new ArrayList<SelectItem>();
     
    	private int idFormat;
    	private String libelle;
     
    	@SuppressWarnings("unchecked")
    	private Map session;
     
        /*
    	 ********************************************************************************************************
    	 * Méthodes                                                                                             *
    	 ********************************************************************************************************
    	 */
    	public String creerFormat() {
    		formatService.createFormat(libelle);
    		return "formatOk";
    	}
     
    	public List<SelectItem> allFormats() {
    		if (tousFormats == null) {
    			try {
    				tousFormats = formatService.getAllFormats();
    			} catch (Exception e) {
    			}
    		}
    		return tousFormats.subList(1, 1);
    	}
     
        public String gererFormat() {
        	tousFormats = null;
        	allFormats();
        	return "test";
    	}
     
        public String modifierFormat() {
        	formatService.editFormat(idFormat, libelle);
    		return "SUCCESS";
    	}
     
        public String supprimerFormat() {
        	formatService.deleteFormat(idFormat);
    		return "SUCCESS";
    	}
     
    	/*
    	 ********************************************************************************************************
    	 * Getters et Setters                                                                                   *
    	 ********************************************************************************************************
    	 */
    	public FormatService getFormatService() {
    		return formatService;
    	}
     
    	public void setFormatService(FormatService service) {
    		formatService = service;
    	}
     
        public List<SelectItem> getTousFormats() {
        	return tousFormats;
    	}
     
    	public void setTousFormats(List<SelectItem> tousFormats) {
    		this.tousFormats = tousFormats;
    	}
     
    	public int getIdFormat() {
    		return idFormat;
    	}
     
    	public void setIdFormat(int idFormat) {
    		this.idFormat = idFormat;
    	}
     
    	public String getLibelle() {
    		return libelle;
    	}
     
    	public void setLibelle(String libelle) {
    		this.libelle = libelle;
    	}
     
     
    	/*
    	 * Autres
    	 */
    	@SuppressWarnings("unchecked")
    	public Map getSession() {
    		return session;
    	}
    	@SuppressWarnings("unchecked")
    	public void setSession(Map session) {
    		this.session = session;
    	}
    }
    -->FormatDAO
    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
    package fr.ducrocql.dao;
    
    import java.util.List;
    
    import javax.faces.model.SelectItem;
    
    import fr.ducrocql.model.Format;
    
    public interface FormatDao {
    	public void saveFormat (Format f);
    	public void removeFormat (Format f);
    	public Format getFormat(int fid);
    	public List<SelectItem> getAll();
    }
    
    -->FormatDAOImpl
    package fr.ducrocql.dao;
    
    import java.util.List;
    
    import javax.faces.model.SelectItem;
    
    import org.hibernate.Session;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import fr.ducrocql.model.Format;
    import fr.ducrocql.utils.HibernateUtil;
    
    public class FormatDaoImpl extends HibernateDaoSupport implements FormatDao {
    
    	public Format getFormat(int fid) {
    		Session session = HibernateUtil.currentSession();
    		Format f = (Format)session.load(Format.class, fid);
    		return f;
    	}
    	
    	public void saveFormat (Format f) {
    		Session session = HibernateUtil.currentSession();
    		session.save(f);
    	}
    	
    	public void removeFormat (Format f) {
    		Session session = HibernateUtil.currentSession();
    		session.delete(f);
    	}
    	
    	@SuppressWarnings("unchecked")
    	public List<SelectItem> getAll() {
    		return (List<SelectItem>) this.getHibernateTemplate().loadAll(Format.class);
    	}
    }
    -->FormatService
    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
    package fr.ducrocql.services;
     
    import java.util.List;
     
    import javax.faces.model.SelectItem;
     
    public interface FormatService {
    	public List<SelectItem> getAllFormats();
     
    	public boolean editFormat(int idFormat, String libelle);
     
    	public boolean deleteFormat(int id);
     
    	public boolean createFormat(String libelle);
    }
    -->FormatServiceImpl
    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
    /*
     * Created on 18 mai 2009
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package fr.ducrocql.services;
     
    import java.util.List;
     
    import javax.faces.model.SelectItem;
     
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
     
    import fr.ducrocql.dao.FormatDao;
    import fr.ducrocql.model.Format;
    import fr.ducrocql.utils.HibernateUtil;
    import fr.ducrocql.utils.Utils;
     
     
    public class FormatServiceImpl implements FormatService {
        private FormatDao FormatDao;
     
    	public List<SelectItem> getAllFormats() {
    		return this.FormatDao.getAll();
    	}
     
    	public boolean editFormat(int fid, String libelle) {
    		Session session = HibernateUtil.currentSession();
    		Transaction tx = null;
    		try {
    			tx = session.beginTransaction();
    			Format f = FormatDao.getFormat(fid);
    			f.setLibelle(libelle);
    			FormatDao.saveFormat(f);
    			tx.commit();
    		}
    		catch(HibernateException e) {
    			e.printStackTrace();
    			if (tx != null) tx.rollback();
    			return false;
    		}
    		HibernateUtil.closeSession();
    		return true;
    	}
     
    	public boolean deleteFormat(int fid) {
    		Session session = HibernateUtil.currentSession();
    		Transaction tx = null;
    		try {
    			tx = session.beginTransaction();
    			Format f = FormatDao.getFormat(fid);
    			FormatDao.removeFormat(f);
    			tx.commit();
    		}
    		catch(HibernateException e) {
    			e.printStackTrace();
    			if (tx != null) tx.rollback();
    			return false;
    		}
    		HibernateUtil.closeSession();
    		return true;
    	}
     
    	public boolean createFormat(String libelle) {
    		Session session = HibernateUtil.currentSession();
    		Transaction tx = null;
    		try {
    			tx = session.beginTransaction();
    			Format f = (Format)Utils.getContext().getBean("Format");
    			f.setLibelle(libelle);
    			FormatDao.saveFormat(f);
    			tx.commit();
    		}
    		catch(HibernateException e) {
    			e.printStackTrace();
    			if (tx != null) tx.rollback();
    			return false;
    		}
    		HibernateUtil.closeSession();
    		return true;
    	}
     
    	public FormatDao getFormatDao() {
    		return FormatDao;
    	}
     
    	public void setFormatDao(FormatDao formatDao) {
    		this.FormatDao = formatDao;
    	}
    }
    pour démarrer, j'exécute index.jsf et je clique sur 'test'.
    J'obtiens alors le message d'erreur :
    An option for component chx was not an instance of javax.faces.model.SelectItem. Type found: java.util.ArrayList.

    NB: la BDD est sous MySQL et j'y accède sans problème.

    Quelqu'un pourrait-il m'aider et m'indiquer la bonne démarche ?

    Merci d'avance pour vos réponses

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    return (List<SelectItem>) this.getHibernateTemplate().loadAll(Format.class);
    Ce code est erroné. Hibernante va, vraisemblablement, te retourner un List<Format> pas un List<SelectItem>. du coup f:selectItems va se casser les dents dessus. Tu dois remplacer ton code par quelque chose de similaire à ceci:



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    formats = (List<Format>) this.getHibernateTemplate().loadAll(Format.class);
    List<SelectItem> result = new ArrayList<SelectItem>();
    for (Format f : formats){
        result.add(new SelectItem(format,format.getLabel());
    }
    return result;

  3. #3
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2009
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2009
    Messages : 19
    Par défaut

    Merci tchize_ pour ta réponse.

    Au prix de quelques modifs de mes sources, ça fonctionne.

    impec !!

  4. #4
    Rédacteur
    Avatar de romaintaz
    Homme Profil pro
    Java craftsman
    Inscrit en
    Juillet 2005
    Messages
    3 790
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Java craftsman
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2005
    Messages : 3 790
    Par défaut
    Citation Envoyé par lducrocq Voir le message

    Merci tchize_ pour ta réponse.

    Au prix de quelques modifs de mes sources, ça fonctionne.

    impec !!
    alors ?
    Nous sommes tous semblables, alors acceptons nos différences !
    --------------------------------------------------------------
    Liens : Blog | Page DVP | Twitter
    Articles : Hudson | Sonar | Outils de builds Java Maven 3 | Play! 1 | TeamCity| CitConf 2009
    Critiques : Apache Maven

  5. #5
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2009
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2009
    Messages : 19
    Par défaut
    Effectivement

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 07/12/2010, 10h42
  2. liste déroulante jsf
    Par amattoallah dans le forum JSF
    Réponses: 3
    Dernier message: 10/06/2010, 15h53
  3. [Bonnes pratiques JSF] liste déroulante
    Par scorplayer dans le forum JSF
    Réponses: 8
    Dernier message: 11/05/2010, 17h23
  4. [JSF] Widget dispo ? Type liste déroulante
    Par MaxLaMenaX dans le forum JSF
    Réponses: 0
    Dernier message: 16/06/2009, 17h40
  5. Réponses: 1
    Dernier message: 20/06/2006, 17h39

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