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 :

[selectONeMenu] Erreur de convertisseur


Sujet :

JSF Java

  1. #1
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut [selectONeMenu] Erreur de convertisseur
    Bonjour,

    J'ai quelques soucis pour utiliser le composant selecftOneMenu
    A partir de cette combo, j'affiche des bean. J'ai une autre combo qui affiche une enum et quand la valeur change, je recharge les autres combo.
    Le problème est que quand je modifie la valeur, j'obtiens le message suivant :

    Erreur de conversion lors de la définition de la valeur "france" pour "null converter"
    Voici ma page :
    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
    <ui:composition template="../../secured/modele/template.xhtml"
    	xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core"
    	xmlns:p="http://primefaces.org/ui"
    	xmlns:ui="http://java.sun.com/jsf/facelets"
    	xmlns:c="http://java.sun.com/jsp/jstl/core">
    	<ui:define name="contenu">
    		<h:form id="ajouterIdentiteForm">
    			<div align="center">
    				<h2>
    					<h:outputText value="#{msg['AjouterUneIdentite']}" />
    				</h2>
    				
    				<p:growl id="messages" showDetail="true" />
    				
    				<p:panelGrid columns="1" style="margin-bottom:15px; border: none;" layout="grid">
    					<p:panelGrid  columns="2" styleClass="panelSansBordure">
    						<!-- Identité -->
    						<h:outputLabel value="#{msg['Identite']} : " styleClass="formulaireTitre" />
    						<h:outputLabel value="" />
    
    						<h:outputLabel for="typePersonne" value="#{msg['Type']} : " styleClass="mandatory" />
    						<p:selectOneMenu id="typePersonne" required="true" immediate="true" onchange="submit();"
    									valueChangeListener="#{ajouterIdentiteForm.reinitialiserIdentite}" value="#{ajouterIdentiteForm.typePersonne}" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listTypePersonnes}" />
            				</p:selectOneMenu>		
    						<h:outputLabel for="nom" id="labelNom" value="#{msg['Nom']} (50) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege == 'P'}" />
    						<p:inputText id="nom" value="#{ajouterIdentiteForm.nom}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    						
    						<h:outputLabel for="prenom" id="labelPrenom" value="#{msg['Prenom']} (50) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    						<p:inputText id="prenom" value="#{ajouterIdentiteForm.prenom}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    		
    						<h:outputLabel for="raisonSociale" id="labelRaisonSociale" value="#{msg['RaisonSociale']} (100) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'M'}" />
    						<p:inputText id="raisonSociale" value="#{ajouterIdentiteForm.raisonSociale}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'M'}" />
    					
    						<!-- Adresse -->
    						<h:outputLabel value="#{msg['Adresse']} : " styleClass="formulaireTitre" />
    						<h:outputLabel value="" />
    						
    						<h:outputLabel for="pays" value="#{msg['Pays']} : " />
    						<p:selectOneMenu id="pays" required="false" value="#{ajouterIdentiteForm.pays}" style="width:400px">
                				                <f:selectItems value="#{ajouterIdentiteForm.listPays}" />
            				        </p:selectOneMenu>        				
            				<h:outputLabel for="canton" value="#{msg['Canton']} : " />
    						<p:selectOneMenu id="canton" required="false" value="#{ajouterIdentiteForm.canton}" style="width:400px">
    							<f:converter converterId="PaysConverter" />
                				<f:selectItems value="#{ajouterIdentiteForm.listCantons}" />
            				</p:selectOneMenu>
    						
    						<h:outputLabel for="rueNumero" value="#{msg['RueEtNumero']} : " />
    						<p:inputText id="rueNumero" value="#{ajouterIdentiteForm .rueNumero}" size="50" maxlength="100" />
    						
    						<h:outputLabel for="casePostale" value="#{msg['CasePostale']} : " />
    						<p:inputText id="casePostale" value="#{ajouterIdentiteForm.casePostale}" size="50" maxlength="100" />
    		
    						<h:outputLabel for="localite" value="#{msg['Localite']} : " />
    						<p:selectOneMenu id="localite" required="false" value="#{ajouterIdentiteForm.localite}" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listLocalites}" />
            				</p:selectOneMenu>
    		
    						<h:outputLabel for="codePostalLibre" value="#{msg['CodePostal']} : " rendered="#{ajouterIdentiteForm.pays.id == 1}" />
    						<p:inputText id="codePostalLibre" value="#{ajouterIdentiteForm.codePostalLibre}" size="50" maxlength="100" rendered="#{ajouterIdentiteForm.pays.id == 1}" />
    		
    						<h:outputLabel for="localiteLibre" value="#{msg['Localite']} : " rendered="#{ajouterIdentiteForm.pays.id != 1}" />
    						<p:inputText id="localiteLibre" value="#{ajouterIdentiteForm.localiteLibre}" size="50" maxlength="100" rendered="#{ajouterIdentiteForm.pays.id != 1}" />
    		
    						<h:outputLabel for="noTelProf" value="#{msg['NoTelProf']} : " />
    						<p:inputText id="noTelProf" value="#{ajouterIdentiteForm.noTelProf}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noTelMobile" value="#{msg['NoTelMobile']} : " />
    						<p:inputText id="noTelMobile" value="#{ajouterIdentiteForm.noTelMobile}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noTelPrive" value="#{msg['NoTelPrive']} : " />
    						<p:inputText id="noTelPrive" value="#{ajouterIdentiteForm.noTelPrive}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="email" value="#{msg['Email']} : " />
    						<p:inputText id="email" value="#{ajouterIdentiteForm.email}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noFax" value="#{msg['NoFax']} : " />
    						<p:inputText id="noFax" value="#{ajouterIdentiteForm.noFax}" size="50" maxlength="50" />
    					</p:panelGrid>
    				</p:panelGrid>
    				
    				<p:commandButton value="#{msg['Ajouter']}" icon="ui-icon-check"
    					style="margin:15px" action="#{ajouterIdentiteForm.ajouterIdentite}" />
    				<p:commandButton value="#{msg['Annuler']}" icon="ui-icon-close"
    					immediate="true" style="margin:15px"
    					action="#{ajouterIdentiteForm.cancel}" />
    			</div>
    		</h:form>
    	</ui:define>
    </ui:composition>
    J'ai essayé d'utiliser un converter mais eclipse met un avertissement :

    PaysConverter' converter id is not registered
    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
     
    @FacesConverter(value = "PaysConverter")
    public class PaysConverter extends BeanConverter {
     
    	@EJB
    	private AdresseService m_adresseService;
     
    	@Override
    	public Object getAsObject(FacesContext context, UIComponent component,
    			String value) {
    		if (value != null) {
    			Pays pays = m_adresseService.getPays(1);
    			return pays;
    		}
     
    		return null;
    	}
     
    }
    Et 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
    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
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
     
    @ManagedBean(name = "ajouterIdentiteForm")
    @RequestScoped
    public class AjouterIdentiteBean {
     
    	private TypePersonneCi typePersonne = TypePersonneCi.PHYSIQUE;
    	private String nom;
    	private String prenom;
    	private String raisonSociale;
    	private Pays pays;
    	private Canton canton;
    	private String rueNumero;
    	private String casePostale;
    	private Localite localite;
    	private String codePostalLibre;
    	private String localiteLibre;
    	private String noTelProf;
    	private String noTelMobile;
    	private String noTelPrive;
    	private String email;
    	private String noFax;
     
    	// Listes
    	private List<TypePersonneCi> listTypePersonnes;
    	private List<Pays> listPays;
    	private List<Canton> listCantons;
     
    	// Services
    	@EJB
    	private IdentiteService m_identiteService;
    	@EJB
    	private AdresseService m_adresseService;
     
    	/**
             * Default Constructor
             */
    	public AjouterIdentiteBean() {
    	}
     
    	public List<TypePersonneCi> getListTypePersonnes() {
    		if (listTypePersonnes == null) {
    			listTypePersonnes = new ArrayList<>();
    			listTypePersonnes.add(TypePersonneCi.PHYSIQUE);
    			listTypePersonnes.add(TypePersonneCi.MORALE);
    		}
     
    		return listTypePersonnes;
    	}
     
    	public List<Pays> getListPays() {
    		if (listPays == null) {
    			listPays = m_adresseService.getPaysList();
    		}
     
    		return listPays;
    	}
     
    	public List<Canton> getListCantons() {
    		if (listCantons == null) {
    			listCantons = m_adresseService.getCantonList();
    		}
     
    		return listCantons;
    	}
     
    	public List<Localite> getListLocalites() {
    		return m_adresseService.getLocaliteListByCanton(m_adresseService.getCanton(1));
    	}
     
    	public void reinitialiserIdentite() {
    		if (TypePersonneCi.PHYSIQUE == typePersonne) {
    			raisonSociale = null;
    		} else {
    			nom = null;
    			prenom = null;
    		}
    	}
     
    	/**
             * Ajouter l'identité dans la base de données
             */
    	public String ajouterIdentite() {
    		try {
    			Identite identite = new Identite();
    			identite.setTypePersonneCi(typePersonne);
    			identite.setNom(nom);
    			identite.setPrenom(prenom);
    			identite.setRaisonSociale(raisonSociale);
     
    			Adresse adresse2 = new Adresse();
    			adresse2.setTypeAdresse(TypeAdresseCi.SECONDAIRE);
    			adresse2.setRueNumero("johns 46+");
    			adresse2.setNoTelProf("bobby");
    			adresse2.setNoTelMobile("bobby");
    			adresse2.setNoTelPrive("bobby");
    			adresse2.setEmail("bobby");
    			adresse2.setNoFax("bobby");
     
    			identite.addAdresse(adresse2);
     
    			Adresse adresse = new Adresse();
    			adresse.setTypeAdresse(TypeAdresseCi.PRINCIPALE);
    			adresse.setRueNumero(rueNumero);
    			adresse.setLocalite(localite);
    			adresse.setNoTelProf(noTelProf);
    			adresse.setNoTelMobile(noTelMobile);
    			adresse.setNoTelPrive(noTelPrive);
    			adresse.setEmail(email);
    			adresse.setNoFax(noFax);
     
    			identite.addAdresse(adresse);
     
    			Adresse adresse1 = new Adresse();
    			adresse1.setTypeAdresse(TypeAdresseCi.SECONDAIRE);
    			adresse1.setRueNumero("bobby 4521");
    			adresse1.setNoTelProf("csac");
    			adresse1.setNoTelMobile("csac");
    			adresse1.setNoTelPrive("csac");
    			adresse1.setEmail("csac");
    			adresse1.setNoFax("csac");
     
    			identite.addAdresse(adresse1);
     
    			m_identiteService.createIdentite(identite);
     
    			FacesContext.getCurrentInstance().addMessage(
    					null,
    					Messages.getMessage("LIdentiteAEteAjouteeAvecSucces",
    							FacesMessage.SEVERITY_INFO, null));
    		} catch (Throwable e) {
    			System.out.println(e);
    			FacesContext.getCurrentInstance().addMessage(null,
    					new FacesMessage(e.getMessage()));
    			return "failure";
    		}
     
    		return "identite";
    	}
     
    	/**
             * Redirige vers la page de gestion des identités
             */
    	public String cancel() {
    		return "identite";
    	}
     
    	public TypePersonneCi getTypePersonne() {
    		return typePersonne;
    	}
     
    	public void setTypePersonne(TypePersonneCi typePersonne) {
    		this.typePersonne = typePersonne;
    	}
     
    	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;
    	}
     
    	public String getRaisonSociale() {
    		return raisonSociale;
    	}
     
    	public void setRaisonSociale(String raisonSociale) {
    		this.raisonSociale = raisonSociale;
    	}
     
    	public Pays getPays() {
    		return pays;
    	}
     
    	public void setPays(Pays pays) {
    		this.pays = pays;
    	}
     
    	public Canton getCanton() {
    		return canton;
    	}
     
    	public void setCanton(Canton canton) {
    		this.canton = canton;
    	}
     
    	public String getRueNumero() {
    		return rueNumero;
    	}
     
    	public void setRueNumero(String rueNumero) {
    		this.rueNumero = rueNumero;
    	}
     
    	public String getCasePostale() {
    		return casePostale;
    	}
     
    	public void setCasePostale(String casePostale) {
    		this.casePostale = casePostale;
    	}
     
    	public Localite getLocalite() {
    		return localite;
    	}
     
    	public void setLocalite(Localite localite) {
    		this.localite = localite;
    	}
     
    	public String getCodePostalLibre() {
    		return codePostalLibre;
    	}
     
    	public void setCodePostalLibre(String codePostalLibre) {
    		this.codePostalLibre = codePostalLibre;
    	}
     
    	public String getLocaliteLibre() {
    		return localiteLibre;
    	}
     
    	public void setLocaliteLibre(String localiteLibre) {
    		this.localiteLibre = localiteLibre;
    	}
     
    	public String getNoTelProf() {
    		return noTelProf;
    	}
     
    	public void setNoTelProf(String noTelProf) {
    		this.noTelProf = noTelProf;
    	}
     
    	public String getNoTelMobile() {
    		return noTelMobile;
    	}
     
    	public void setNoTelMobile(String noTelMobile) {
    		this.noTelMobile = noTelMobile;
    	}
     
    	public String getNoTelPrive() {
    		return noTelPrive;
    	}
     
    	public void setNoTelPrive(String noTelPrive) {
    		this.noTelPrive = noTelPrive;
    	}
     
    	public String getEmail() {
    		return email;
    	}
     
    	public void setEmail(String email) {
    		this.email = email;
    	}
     
    	public String getNoFax() {
    		return noFax;
    	}
     
    	public void setNoFax(String noFax) {
    		this.noFax = noFax;
    	}
     
    	public IdentiteService getM_identiteService() {
    		return m_identiteService;
    	}
     
    	public void setM_identiteService(IdentiteService m_identiteService) {
    		this.m_identiteService = m_identiteService;
    	}
     
    }
    Qqun peut-il m'aider svp ?
    merci d'avance

  2. #2
    Membre chevronné
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Points : 1 993
    Points
    1 993
    Par défaut
    On dirait qu'il y a une quote perdue à la fin du nom de ton converter (ce qui pourrait expliquer qu'il ne trouve pas)
    Je ne suis pas mort, j'ai du travail !

  3. #3
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Ou c'est que tu vois une quote perdue ? Je ne vois pas ....

  4. #4
    Membre éprouvé Avatar de Lady
    Femme Profil pro
    Développeur Java
    Inscrit en
    Mars 2003
    Messages
    678
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2003
    Messages : 678
    Points : 909
    Points
    909
    Par défaut
    Je ne vois nulle part l'endroit où tu défini que le converter est à utiliser pour tes objets pays
    Informaticienne le jour, créatrice de bijoux la nuit (https://www.facebook.com/La-Fée-Chro...07539656306271) et maman à plein temps !

  5. #5
    Membre chevronné
    Avatar de eulbobo
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2003
    Messages
    786
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2003
    Messages : 786
    Points : 1 993
    Points
    1 993
    Par défaut
    Là :
    PaysConverter' converter id is not registered
    Je ne suis pas mort, j'ai du travail !

  6. #6
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Quel rapport entre Canton et PaysConverter ?
    Tu n'aurais pas mis au mauvais endroit ton converter ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 13
    Points : 14
    Points
    14
    Par défaut
    D'après le code, le converter doit être un sous composant de la liste pays et non du canton.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <p:selectOneMenu id="pays" required="false" value="#{ajouterIdentiteForm.pays}" style="width:400px">
       <f:converter converterId="PaysConverter" />
       <f:selectItems value="#{ajouterIdentiteForm.listPays}" />
    </p:selectOneMenu>
    Sinon dans le converter quand la value n'est pas nulle vous renvoyez toujours le même pays en attaquant le métier.
    ligne12 : Pays pays = m_adresseService.getPays(1);
    Alors que l'objet pays vous l'obtenez grâce à la liste déroulante de pays. Le converter sert à ça aussi. Ne être obligé de passer par le métier et un appel base de données pour obtenir le pays sélectionné dans la liste.

  8. #8
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Bonjour,

    Merci beaucoup pour vos réponses.
    >Effectivement, je l'avais mal placé

    Maintenant ça fonctionne. Par contre smile_caki :
    Sinon dans le converter quand la value n'est pas nulle vous renvoyez toujours le même pays en attaquant le métier.

    ligne12 : Pays pays = m_adresseService.getPays(1);
    Alors que l'objet pays vous l'obtenez grâce à la liste déroulante de pays. Le converter sert à ça aussi. Ne être obligé de passer par le métier et un appel base de données pour obtenir le pays sélectionné dans la liste.
    Comment je peux récupérer le pays sans passer par la BD, grâce à la liste déroulante ? Je ne vois pas comment faire ça

    J'ai juste encore un petit problème avec ma page, à savoir que le champ "raison sociale" de mon managedBean n'est jamais renseigné, malgré le fait d'y saisir une valeur.
    Est-ce que ça vient de "immediate = true" du champ type personne et de la portée de mon bean ? (le but étant de cacher / afficher les champs (nom, prenom, raison sociale) en fonction du type de personne (physique, morale)
    Qqun a-t-il une idée ?
    Voici le fichier actuel :
    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
    <ui:composition template="../../secured/modele/template.xhtml"
    	xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core"
    	xmlns:p="http://primefaces.org/ui"
    	xmlns:ui="http://java.sun.com/jsf/facelets"
    	xmlns:c="http://java.sun.com/jsp/jstl/core">
    	<ui:define name="contenu">
    		<h:form id="ajouterIdentiteForm">
    			<div align="center">
    				<h2>
    					<h:outputText value="#{msg['AjouterUneIdentite']}" />
    				</h2>
    				
    				<p:growl id="messages" showDetail="true" />
    				
    				<p:panelGrid columns="1" style="margin-bottom:15px; border: none;" layout="grid">
    					<p:panelGrid  columns="2" styleClass="panelSansBordure">
    						<!-- Identité -->
    						<h:outputLabel value="#{msg['Identite']} : " styleClass="formulaireTitre" />
    						<h:outputLabel value="" />
    
    						<h:outputLabel for="typePersonne" value="#{msg['Type']} : " styleClass="mandatory" />
    						<p:selectOneMenu id="typePersonne" required="true" immediate="true" onchange="submit();"
    								 value="#{ajouterIdentiteForm.typePersonne}" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listTypePersonnes}" />
            				</p:selectOneMenu>
    		
    						<h:outputLabel for="nom" id="labelNom" value="#{msg['Nom']} (50) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege == 'P'}" />
    						<p:inputText id="nom" value="#{ajouterIdentiteForm.nom}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    						
    						<h:outputLabel for="prenom" id="labelPrenom" value="#{msg['Prenom']} (50) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    						<p:inputText id="prenom" value="#{ajouterIdentiteForm.prenom}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'P'}" />
    		
    						<h:outputLabel for="raisonSociale" id="labelRaisonSociale" value="#{msg['RaisonSociale']} (100) : " styleClass="mandatory" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'M'}" />
    						<p:inputText id="raisonSociale" value="#{ajouterIdentiteForm.raisonSociale}" size="50" maxlength="50" rendered="#{ajouterIdentiteForm.typePersonne.abrege eq 'M'}" />					
    						<!-- Adresse -->
    						<h:outputLabel value="#{msg['Adresse']} : " styleClass="formulaireTitre" />
    						<h:outputLabel value="" />
    						
    						<h:outputLabel for="pays" value="#{msg['Pays']} : " />
    						<p:selectOneMenu id="pays" required="false" value="#{ajouterIdentiteForm.pays}" converter="PaysConverter" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listPays}" />
            				</p:selectOneMenu>
            				
            				<h:outputLabel for="canton" value="#{msg['Canton']} : " />
    						<p:selectOneMenu id="canton" required="false" value="#{ajouterIdentiteForm.canton}" converter="CantonConverter" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listCantons}" />
            				</p:selectOneMenu>
    						
    						<h:outputLabel for="rueNumero" value="#{msg['RueEtNumero']} : " />
    						<p:inputText id="rueNumero" value="#{ajouterIdentiteForm .rueNumero}" size="50" maxlength="100" />
    						
    						<h:outputLabel for="casePostale" value="#{msg['CasePostale']} : " />
    						<p:inputText id="casePostale" value="#{ajouterIdentiteForm.casePostale}" size="50" maxlength="100" />
    		
    						<h:outputLabel for="localite" value="#{msg['Localite']} : " />
    						<p:selectOneMenu id="localite" required="false" value="#{ajouterIdentiteForm.localite}" converter="LocaliteConverter" style="width:400px">
                				<f:selectItems value="#{ajouterIdentiteForm.listLocalites}" />
            				</p:selectOneMenu>
    		
    						<h:outputLabel for="codePostalLibre" value="#{msg['CodePostal']} : " rendered="#{ajouterIdentiteForm.pays.id == 1}" />
    						<p:inputText id="codePostalLibre" value="#{ajouterIdentiteForm.codePostalLibre}" size="50" maxlength="100" rendered="#{ajouterIdentiteForm.pays.id == 1}" />
    		
    						<h:outputLabel for="localiteLibre" value="#{msg['Localite']} : " rendered="#{ajouterIdentiteForm.pays.id != 1}" />
    						<p:inputText id="localiteLibre" value="#{ajouterIdentiteForm.localiteLibre}" size="50" maxlength="100" rendered="#{ajouterIdentiteForm.pays.id != 1}" />
    		
    						<h:outputLabel for="noTelProf" value="#{msg['NoTelProf']} : " />
    						<p:inputText id="noTelProf" value="#{ajouterIdentiteForm.noTelProf}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noTelMobile" value="#{msg['NoTelMobile']} : " />
    						<p:inputText id="noTelMobile" value="#{ajouterIdentiteForm.noTelMobile}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noTelPrive" value="#{msg['NoTelPrive']} : " />
    						<p:inputText id="noTelPrive" value="#{ajouterIdentiteForm.noTelPrive}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="email" value="#{msg['Email']} : " />
    						<p:inputText id="email" value="#{ajouterIdentiteForm.email}" size="50" maxlength="50" />
    						
    						<h:outputLabel for="noFax" value="#{msg['NoFax']} : " />
    						<p:inputText id="noFax" value="#{ajouterIdentiteForm.noFax}" size="50" maxlength="50" />
    					</p:panelGrid>
    				</p:panelGrid>
    				
    				<p:commandButton value="#{msg['Ajouter']}" icon="ui-icon-check"
    					style="margin:15px" action="#{ajouterIdentiteForm.ajouterIdentite}" />
    				<p:commandButton value="#{msg['Annuler']}" icon="ui-icon-close"
    					immediate="true" style="margin:15px"
    					action="#{ajouterIdentiteForm.cancel}" />
    			</div>
    		</h:form>
    	</ui:define>
    </ui:composition>
    Et le bean :
    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
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
     
    @ManagedBean(name = "ajouterIdentiteForm")
    @RequestScoped
    public class AjouterIdentiteBean {
     
    	private TypePersonneCi typePersonne = TypePersonneCi.PHYSIQUE;
    	private String nom;
    	private String prenom;
    	private String raisonSociale;
    	private Pays pays;
    	private Canton canton;
    	private String rueNumero;
    	private String casePostale;
    	private Localite localite;
    	private String codePostalLibre;
    	private String localiteLibre;
    	private String noTelProf;
    	private String noTelMobile;
    	private String noTelPrive;
    	private String email;
    	private String noFax;
     
    	// Listes
    	private List<TypePersonneCi> listTypePersonnes;
    	private List<Pays> listPays;
    	private List<Canton> listCantons;
     
    	// Services
    	@EJB
    	private IdentiteService m_identiteService;
    	@EJB
    	private AdresseService m_adresseService;
     
    	/**
             * Default Constructor
             */
    	public AjouterIdentiteBean() {
    	}
     
    	public List<TypePersonneCi> getListTypePersonnes() {
    		if (listTypePersonnes == null) {
    			listTypePersonnes = new ArrayList<>();
    			listTypePersonnes.add(TypePersonneCi.PHYSIQUE);
    			listTypePersonnes.add(TypePersonneCi.MORALE);
    		}
     
    		return listTypePersonnes;
    	}
     
    	public List<Pays> getListPays() {
    		if (listPays == null) {
    			listPays = m_adresseService.getPaysList();
    		}
     
    		return listPays;
    	}
     
    	public List<Canton> getListCantons() {
    		if (listCantons == null) {
    			listCantons = m_adresseService.getCantonList();
    		}
     
    		return listCantons;
    	}
     
    	public List<Localite> getListLocalites() {
    		return m_adresseService.getLocaliteListByCanton(m_adresseService.getCanton(1));
    	}
     
    	public void reinitialiserIdentite() {
    		if (TypePersonneCi.PHYSIQUE == typePersonne) {
    			raisonSociale = null;
    		} else {
    			nom = null;
    			prenom = null;
    		}
    	}
     
    	/**
             * Ajouter l'identité dans la base de données
             */
    	public String ajouterIdentite() {
    		try {
    			Identite identite = new Identite();
    			identite.setTypePersonneCi(typePersonne);
    			identite.setNom(nom);
    			identite.setPrenom(prenom);
    			identite.setRaisonSociale(raisonSociale);
     
    			Adresse adresse = new Adresse();
    			adresse.setTypeAdresse(TypeAdresseCi.PRINCIPALE);
    			adresse.setPays(pays);
    			adresse.setRueNumero(rueNumero);
    			adresse.setLocalite(localite);
    			adresse.setNoTelProf(noTelProf);
    			adresse.setNoTelMobile(noTelMobile);
    			adresse.setNoTelPrive(noTelPrive);
    			adresse.setEmail(email);
    			adresse.setNoFax(noFax);
     
    			identite.addAdresse(adresse);
     
    			m_identiteService.createIdentite(identite);
     
    			FacesContext.getCurrentInstance().addMessage(
    					null,
    					Messages.getMessage("LIdentiteAEteAjouteeAvecSucces",
    							FacesMessage.SEVERITY_INFO, null));
    		} catch (Throwable e) {
    			System.out.println(e);
    			FacesContext.getCurrentInstance().addMessage(null,
    					new FacesMessage(e.getMessage()));
    			return "failure";
    		}
     
    		return "identite";
    	}
     
    	/**
             * Redirige vers la page de gestion des identités
             */
    	public String cancel() {
    		return "identite";
    	}
     
    	public TypePersonneCi getTypePersonne() {
    		return typePersonne;
    	}
     
    	public void setTypePersonne(TypePersonneCi typePersonne) {
    		this.typePersonne = typePersonne;
    	}
     
    	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;
    	}
     
    	public String getRaisonSociale() {
    		return raisonSociale;
    	}
     
    	public void setRaisonSociale(String raisonSociale) {
    		this.raisonSociale = raisonSociale;
    	}
     
    	public Pays getPays() {
    		return pays;
    	}
     
    	public void setPays(Pays pays) {
    		this.pays = pays;
    	}
     
    	public Canton getCanton() {
    		return canton;
    	}
     
    	public void setCanton(Canton canton) {
    		this.canton = canton;
    	}
     
    	public String getRueNumero() {
    		return rueNumero;
    	}
     
    	public void setRueNumero(String rueNumero) {
    		this.rueNumero = rueNumero;
    	}
     
    	public String getCasePostale() {
    		return casePostale;
    	}
     
    	public void setCasePostale(String casePostale) {
    		this.casePostale = casePostale;
    	}
     
    	public Localite getLocalite() {
    		return localite;
    	}
     
    	public void setLocalite(Localite localite) {
    		this.localite = localite;
    	}
     
    	public String getCodePostalLibre() {
    		return codePostalLibre;
    	}
     
    	public void setCodePostalLibre(String codePostalLibre) {
    		this.codePostalLibre = codePostalLibre;
    	}
     
    	public String getLocaliteLibre() {
    		return localiteLibre;
    	}
     
    	public void setLocaliteLibre(String localiteLibre) {
    		this.localiteLibre = localiteLibre;
    	}
     
    	public String getNoTelProf() {
    		return noTelProf;
    	}
     
    	public void setNoTelProf(String noTelProf) {
    		this.noTelProf = noTelProf;
    	}
     
    	public String getNoTelMobile() {
    		return noTelMobile;
    	}
     
    	public void setNoTelMobile(String noTelMobile) {
    		this.noTelMobile = noTelMobile;
    	}
     
    	public String getNoTelPrive() {
    		return noTelPrive;
    	}
     
    	public void setNoTelPrive(String noTelPrive) {
    		this.noTelPrive = noTelPrive;
    	}
     
    	public String getEmail() {
    		return email;
    	}
     
    	public void setEmail(String email) {
    		this.email = email;
    	}
     
    	public String getNoFax() {
    		return noFax;
    	}
     
    	public void setNoFax(String noFax) {
    		this.noFax = noFax;
    	}
     
    	public IdentiteService getM_identiteService() {
    		return m_identiteService;
    	}
     
    	public void setM_identiteService(IdentiteService m_identiteService) {
    		this.m_identiteService = m_identiteService;
    	}
     
    }
    Encore un grand merci à tous pour votre aide

  9. #9
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 13
    Points : 14
    Points
    14
    Par défaut
    Pour répondre à ta question:
    Comment je peux récupérer le pays sans passer par la BD, grâce à la liste déroulante ? Je ne vois pas comment faire ça
    Dans ton backing bean, tu as le code suivant pour retourner la liste des pays que tu obtiens par le métier:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public List<Pays> getListPays() {
    	if (listPays == null) {
    		listPays = m_adresseService.getPaysList();
    	}
     
    	return listPays;
    }
    Transforme cette liste en Map<String, Pays> avec comme clé l'id du pays (avec comme type String si c'est un String). Cette transformation tu la fait dans le métier (ce qui change le type de retour de ta méthode).

    Exemple de code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public Map<String, Pays> findAll()
    {
    	Query findAll = entityManager.createNamedQuery("Pays.findAll");
    	List<Pays> lResultList = findAll.getResultList();
     
    	Map<String, Pays> mapResult = new HashMap<String, Pays>();
    	for (Pays lPays : lResultList)
    	{
    		mapResult.put(String.valueOf(lPays.getId()), lPays);
    	}
    	return mapResult;
    }
    Dans ton backing bean, tu ne changes rien à part le type de ta liste.

    Dans ta JSF, sur la liste selectOneMenu tu fais appel au converter et ajoute ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <p:selectOneMenu id="pays" required="false" value="#{ajouterIdentiteForm.pays}" converter="PaysConverter" style="width:400px">
           <f:selectItems value="#{ajouterIdentiteForm.listPays}" var="pays" itemLabel="#{pays.libelle}" itemValue="#{pays}" />
    </p:selectOneMenu>
    Ton converter ressemble alors à ceci:
    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
    @Override
       public Object getAsObject(FacesContext context, UIComponent component, String value)
       {
          if (value != null && value.trim().length() > 0)
          {
             logger.info("Obtenir sur le scope application le managed bean des pays");
             PaysManagedBean lPaysMB = context.getApplication().evaluateExpressionGet(context, "#{PaysManagedBean}", PaysManagedBean.class);
             return lPaysMB.getPaysList().get(value);
          }
          else
          {
             logger.info("La value est nulle");
             return null;
          }
       }
     
       @Override
       public String getAsString(FacesContext context, UIComponent component, Object object)
       {
          if (object != null)
          {
             logger.info("Obtenir sur l'objet Pays son identifiant");
             return String.valueOf(((Pays) object).getId());
          }
          else
          {
             logger.info("L'objet est nul");
             return null;
          }
    C'est un extrait de code que j'ai utilisé et que j'ai adapté à ton cas. Pour mon cas, j'ai un backing bean Pays, à part et de scope @ApplicationScoped car c'est le même pour toute la durée de mon application (table de référence), que j'injecte dans le backing bean de ma vue.
    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
    @Named
    @ApplicationScoped
    public class PaysManagedBean
    {
    	private Map<String, Pays> paysList;
    	private static Logger logger = Logger.getLogger(PaysManagedBean.class.getName());
     
    	@EJB
    	private PaysDao paysDao;
     
    	public PaysManagedBean()
    	{
    	}
     
    	@PostConstruct
    	public void init()
    	{
    		if (getPaysList() == null)
    		{
    			logger.info("Scope APPLI >> Chargement des pays");
    			setPaysList(paysDao.findAll());
    		}
    	}
    Ainsi ma liste de pays est chargée au premier appel puis reste en mémoire pendant la durée de vie de l'appli. Le converter permet alors d'aller chercher dans la liste n'importe quel pays à partir de son identifiant sans faire appel à la base de données.

  10. #10
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 13
    Points : 14
    Points
    14
    Par défaut
    Pour ton problème de raison sociale : as-tu essayé de mettre un valueChangeListener="#{ajouterIdentiteForm.typePersonneChanged}" en plus du onchange="submit();"
    Avec dans ton backing bean la méthode suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public void typePersonneChanged(ValueChangeEvent pEvent)
    {
    	// ton code pour renseigner la raison sociale
    }
    Tu devras peut-être aussi changer le scope de ton backing bean en le mettant en @ViewScoped.

  11. #11
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Super, merci beaucoup pour ces explications. C'est très clair, merci d'avoir pris le temps d'expliquer tout ça.
    Ca fonctionne mais il y a juste un petit truc que j'aimerais encore régler.

    Actuellement, le converter gère les pays par leur nom. C'est à dire qu'il récupère comme "value" le nom (par exemple France) et recherche dans la map, la clé "France" or j'aimerais qu'il gère l'ID qui lui est unique (bon le nom du pays aussi en principe, mais dans un autre cas, il se pourrait que ce ne soit pas le cas. De plus dans une appli multi-langue, ça ne marcherait pas).

    Poue cela, j'ai essayé d'adapter le code, mais si je fais ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <h:outputLabel for="pays" value="#{msg['Pays']} : " />
    <p:selectOneMenu id="pays" required="false" immediate="true" onchange="submit();" value="#{saisirIdentiteForm.pays}" converter="PaysConverter" style="width:400px">
             <f:selectItems value="#{saisirIdentiteForm.listPays}" var="pays" itemLabel="#{pays.nom}" itemValue="#{pays.id}" />
    </p:selectOneMenu>
    J'obtiens cette erreur :
    2015-06-17T08:37:10.817+0200|Avertissement: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
    java.lang.ClassCastException: java.lang.Integer cannot be cast to application.beans.identite.adresse.Pays
    at application.beans.identite.adresse.Pays.equals(Pays.java:50)
    at org.primefaces.renderkit.SelectRenderer.isSelected(SelectRenderer.java:59)
    ..........
    Si je fais ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <h:outputLabel for="pays" value="#{msg['Pays']} : " />
    <p:selectOneMenu id="pays" required="false" immediate="true" onchange="submit();" value="#{saisirIdentiteForm.pays}" converter="PaysConverter" style="width:400px">
         <f:selectItems value="#{saisirIdentiteForm.listPays}" var="pays" itemLabel="#{pays.id}" itemValue="#{pays}" />
    </p:selectOneMenu>
    Il affiche l'ID dans la combo, mais la "value" du converter est toujours le nom du pays (en plus du fait qu'il n'est pas envisageable d'afficher l'ID à l'écran au lieu du nom)

    As-tu une idée pour gérer ça ?
    Encore merci pour ton aide, bien précieuse

  12. #12
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    peux-tu nous donner la ligne:
    at application.beans.identite.adresse.Pays.equals(Pays.java:50)

    ou tout simplement le code de la classe Pays.java

    Eric

  13. #13
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Je suppose que la méthode toString() renvoie le libellé du pays, c'est ce qui expliquerait que itemValue="#{pays}" renvoie ça
    Il faudrait faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <f:selectItems value="#{saisirIdentiteForm.listPays}" var="pays" itemLabel="#{pays.libelle}" itemValue="#{pays.id}" />
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  14. #14
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Si je mets ça, j'ai la même erreur qu'avant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <f:selectItems value="#{saisirIdentiteForm.listPays}" var="pays" itemLabel="#{pays.libelle}" itemValue="#{pays.id}" />
    Voici le code de Pays, c'est la méthode equals qui plante car il veut caster un Integer en Pays ...
    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
     
    public class Pays extends BusinessEntite {
     
    	@Column(name = "SEQ_TRI", nullable = false)
    	private int seqTri;
     
    	@Column(name = "ABREGE", length = 10, nullable = false, unique = true)
    	private String abrege;
     
    	@Column(name = "NOM", length = 50, nullable = false)
    	private String nom;
     
    	/**
             * Default Constructor
             */
    	public Pays() {
    	}
     
    	@Override
    	public String toString() {
    		return getNom();
    	}
     
    	@Override
    	public boolean equals(Object obj) {
    		Pays pays = (Pays) obj;
    		return obj == this || pays.getId() == this.getId();
    	}
     
    	@Override
    	public int hashCode() {
    		return super.hashCode();
    	}
     
    	public int getSeqTri() {
    		return seqTri;
    	}
     
    	public void setSeqTri(int seqTri) {
    		this.seqTri = seqTri;
    	}
     
    	public String getAbrege() {
    		return abrege;
    	}
     
    	public void setAbrege(String abrege) {
    		this.abrege = abrege;
    	}
     
    	public String getNom() {
    		return nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}	
    }

  15. #15
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Peux-tu mettre le code de ton converter ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  16. #16
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Bien sûr, voici le converter :
    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
     
    @FacesConverter(value = "PaysConverter")
    public class PaysConverter extends BeanConverter {
     
    	@EJB
    	private AdresseService m_adresseService;
     
    	@Override
    	public Object getAsObject(FacesContext context, UIComponent component,
    			String value) {
     
    		if (value != null && value.trim().length() > 0) {
    			PaysManagedBean paysMB = context.getApplication()
    					.evaluateExpressionGet(context, "#{paysManagedBean}",
    							PaysManagedBean.class);
    			return paysMB.getMapPays().get(value);
    		} else {
    			return null;
    		}
    	}
     
    }
    Et le PaysManagedBean :
    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
     
    @ManagedBean(name = "paysManagedBean")
    @ApplicationScoped
    public class PaysManagedBean {
     
    	private Map<Integer, Pays> mapPays;
     
    	@EJB
    	private AdresseService m_adresseService;
     
    	/**
             * Default Constructor
             */
    	public PaysManagedBean() {
    	}
     
    	@PostConstruct
    	public void init() {
    		if (getMapPays() == null) {
    			setMapPays(m_adresseService.getPaysMap());
    		}
    	}
     
    	public Map<Integer, Pays> getMapPays() {
    		return mapPays;
    	}
     
    	public void setMapPays(Map<Integer, Pays> mapPays) {
    		this.mapPays = mapPays;
    	}
    }

  17. #17
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Une remarque en passant, tu n'as pas besoin de l'EJB dans ton converter, il n'est visiblement pas utilisé.

    Ta Map a bien comme clé l'Integer id de Pays ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  18. #18
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Ha oui, j'ai oublié de l'enlever, merci
    Oui la map contient bien l'id
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private Map<Integer, Pays> mapPays;
    Le problème est que "value" du converter contient le nom du pays

  19. #19
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Alors là, j'ai du mal à comprendre comment c'est possible si tu as mis ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <f:selectItems value="#{saisirIdentiteForm.listPays}" var="pays" itemLabel="#{pays.libelle}" itemValue="#{pays.id}" />
    Tu n'aurais pas un problème de déploiement de ton serveur ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  20. #20
    Membre actif
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Points : 270
    Points
    270
    Par défaut
    Alors effectivement j'avais encore
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    itemValue="#{pays.nom}"
    qui trainait suite aux derniers tests.
    Par contre, si je mets , j'ai l'erreur de cast suivante dans l'objet Pays (il ne peut pas caster un Integer en pays):
    2015-06-18T10:59:05.323+0200|Avertissement: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
    java.lang.ClassCastException: java.lang.Integer cannot be cast to application.beans.identite.adresse.Pays
    at application.beans.identite.adresse.Pays.equals(Pays.java:50)
    at org.primefaces.renderkit.SelectRenderer.isSelected(SelectRenderer.java:59)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeOption(SelectOneMenuRenderer.java:369)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeSelectItems(SelectOneMenuRenderer.java:342)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeInput(SelectOneMenuRenderer.java:140)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:93)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:67)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:127)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableBody(PanelGridRenderer.java:98)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableLayout(PanelGridRenderer.java:65)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:37)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeGridBody(PanelGridRenderer.java:223)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeGridLayout(PanelGridRenderer.java:86)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:40)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:79)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:66)
    at org.primefaces.component.outputpanel.OutputPanelRenderer.encodeMarkup(OutputPanelRenderer.java:65)
    at org.primefaces.component.outputpanel.OutputPanelRenderer.encodeEnd(OutputPanelRenderer.java:41)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:83)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:66)
    at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at application.filters.LoginFilter.doFilter(LoginFilter.java:50)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
    Et si je mets "value" est également un String

    Chose que je ne comprends pas, pourquoi le cast pète avec un Integer (id) et pas avec uns string (nom) ?

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. selectOneMenu Erreur de validation: Valeur not valid
    Par raiseyourfist59 dans le forum JSF
    Réponses: 1
    Dernier message: 29/02/2016, 11h47
  2. Réponses: 11
    Dernier message: 28/01/2012, 11h48
  3. erreur remplissage d'un selectOneMenu
    Par zerocoolyoussef dans le forum JSF
    Réponses: 10
    Dernier message: 20/02/2009, 16h22
  4. [ICEFACES] selectOneMenu Erreur de Validation
    Par *alexandre* dans le forum JSF
    Réponses: 5
    Dernier message: 19/01/2009, 17h45
  5. erreur remplissage d'un selectOneMenu
    Par mimaaaa dans le forum JSF
    Réponses: 9
    Dernier message: 04/09/2008, 14h43

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