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 :

[Affichage d'une liste d'objets sur une page JSF à partir d'un bean]


Sujet :

JSF Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2018
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Conseil

    Informations forums :
    Inscription : Janvier 2018
    Messages : 3
    Points : 2
    Points
    2
    Par défaut [Affichage d'une liste d'objets sur une page JSF à partir d'un bean]
    Bonjour,

    Je me permets de vous solliciter dans la cadre de la réalisation d'une application de gestion de contacts basique (CRUD).

    J'ai un bean "Contact" dans lequel il y a :
    - Plusieurs attributs qui concernent le contact
    - Un contructeur vide
    - Un constructeur paramétré
    - Les accesseurs (getters/setters)
    - Une méthode qui récupère le résultat du select des contacts dans la base de données (appel de la méthode du DAO) dans une ArrayList<Contact>. Cette méthode renvoie cette ArrayList<Contact>. (Cette ArrayList n'est pas vide !)

    Le soucis est le suivant :

    Dans ma page consacrée à l'affichage des contacts, j'ai ce bout de code qui s'occupe d'afficher les contacts mais il affiche uniquement le libellé ID sans la valeur.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <h:dataTable value = "#{contact.méthode()}" var = "c">
       <h:column>    				
          <f:facet name = "header">Id</f:facet>    				
          <h:outputText value="#{c.contactid}"></h:ouputText>
     </h:column>
    </h:dataTable>
    Pourriez-vous m'éclairer s'il vous plait ?

    Merci par avance.

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

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 311
    Points : 9 524
    Points
    9 524
    Billets dans le blog
    1
    Par défaut
    Difficile de t'aider avec aussi peu de code...
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Candidat au Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2018
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Conseil

    Informations forums :
    Inscription : Janvier 2018
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Voici le code du bean Contact :

    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
     
     
    package bean;
     
    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import javax.annotation.PostConstruct;
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    import javax.naming.NamingException;
    import service.ServiceContact;
     
    @ManagedBean(name = "contact") // Concentré d'actionForm et action
    @SessionScoped
     
    public class Contact implements Serializable {
    	// login et password du User
    	private static final long serialVersionUID = 1L;
    	private int contactId;
    	private String firstName;
    	private String lastName;
    	private String email;
    	private String street;
    	private String city;
    	private String zip;
    	private String country;
    	private Address contactAddress;
     
    	public int getContactId() {
    		return contactId;
    	}
     
    	public void setContactId(int contactId) {
    		this.contactId = contactId;
    	}
     
    	public String getFirstName() {
    		return this.firstName;
    	}
     
    	public void setFirstName(String name) {
    		this.firstName = name;
    	}
     
    	public String getLastName() {
    		return this.lastName;
    	}
     
    	public void setLastName(String name) {
    		this.lastName = name;
    	}
     
    	public String getEmail() {
    		return this.email;
    	}
     
    	public void setEmail(String email) {
    		this.email = email;
    	}
     
    	public String getStreet() {
    		return street;
    	}
     
    	public void setStreet(String street) {
    		this.street = street;
    	}
     
    	public String getCity() {
    		return city;
    	}
     
    	public void setCity(String city) {
    		this.city = city;
    	}
     
    	public String getCountry() {
    		return country;
    	}
     
    	public void setCountry(String country) {
    		this.country = country;
    	}
     
    	public String getZip() {
    		return zip;
    	}
     
    	public void setZip(String zip) {
    		this.zip = zip;
    	}
     
    	public Address getContactAddress() {
    		return contactAddress;
    	}
     
    	public void setContactAddress(Address contactAddress) {
    		this.contactAddress = contactAddress;
    	}
     
    	public Contact(){
     
    	}
     
    	public Contact(int contactId, String firstName, String lastName, String email, Address contactAddress){
    		this.contactId = contactId;
    		this.firstName= firstName;
    		this.lastName = lastName;
    		this.email = email;
    		this.contactAddress = contactAddress;
    	}
     
     
    	public String checkContact() throws NamingException, SQLException {
    		// Gestion des messages d'erreurs
    		FacesContext context = FacesContext.getCurrentInstance();
     
    		if (isMissing(firstName) || isMissing(lastName) || isMissing(email) || isMissing(street) || isMissing(city)
    				|| isMissing(zip) || isMissing(country)) {
    			if (isMissing(firstName)) {
    				context.addMessage(null, new FacesMessage("Prénom requis"));
    			}
     
    			if (isMissing(lastName)) {
    				context.addMessage(null, new FacesMessage("Nom requis"));
    			}
     
    			if (isMissing(email)) {
    				context.addMessage(null, new FacesMessage("Mail requis"));
    			}
     
    			if (isMissing(street)) {
    				context.addMessage(null, new FacesMessage("Rue requis"));
    			}
     
    			if (isMissing(city)) {
    				context.addMessage(null, new FacesMessage("Ville requis"));
    			}
     
    			if (isMissing(zip)) {
    				context.addMessage(null, new FacesMessage("Code postal requis"));
    			}
     
    			if (isMissing(country)) {
    				context.addMessage(null, new FacesMessage("Pays requis"));
    			}
     
    			return null;
    		} else {
    			final ServiceContact Service = new ServiceContact();
     
    			//Ajout de l'adresse du contact
    			final String addressError = Service.addAddress(street, city, zip, country);
     
    			//Récupère l'identifiant de l'adresse pour pouvoir ajouter l'adresse au contact
    			final int idAddress = Service.getAddressId(street, city, zip, country);
     
    			//Ajout du contact
    			final String contactError = Service.addContact(firstName, lastName, email, idAddress);
     
    			//Redirection en fonction des erreurs de traitement
    			if (addressError==null|| idAddress != 0 || contactError==null) {
    				return ("menu");
    			} else {
    				return ("login");
    			}
    		}
    	}
     
    	@PostConstruct
    	public ArrayList<Contact> displayContacts(){
    		//Show contacts
    		final ServiceContact lDAOContact = new ServiceContact();
    		final ArrayList<Contact> Contacts = lDAOContact.getContacts();
    		return Contacts;
    	}
     
    	// Contrôle que les champs sont non-vides
    	private boolean isMissing(String value) {
    		return ((value == null) || (value.trim().isEmpty()));
    	}
    }

    Voici le code JSP où je veux que l'application web affiche les contacts :

    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/"
    	xmlns:f="http://java.sun.com/jsf/core">
     
    <h:head>
    	<link rel="stylesheet" type="text/css"
    		href="//cdn.datatables.net/1.10.0/css/jquery.dataTables.css" />
    	<title>Contact</title>
    </h:head>
     
    <h:body>
    	<h1>Gestion des contacts</h1>
    	<h:dataTable value="#{contact.displayContacts()}" var="co">
    			<h:column>
    				<f:facet name="header">ID</f:facet>
    				<h:outputText value="#{co.contactId}"></h:outputText>
    			</h:column>	
    	</h:dataTable>
    </h:body>
     
    </html>
    Le code du DAO qui s'occupe de récupéré les contacts qu'il trouve dans la base de données :

    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
     
    public static ArrayList<Contact> getContacts() {
    		ArrayList<Contact> Contacts = new ArrayList<Contact>(); 
    		try {
    			final Context lContext = new InitialContext();
    			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
    			final Connection lConnection  = lDataSource.getConnection();
     
    			final PreparedStatement lPreparedStatementCreation = lConnection.prepareStatement("SELECT * FROM CONTACT C, ADDRESS A WHERE C.ID_ADDRESS=A.ID");
    			ResultSet rs = lPreparedStatementCreation.executeQuery();
     
    			while (rs.next()) {
    				int contactid = rs.getInt("ID_CONTACT");
    				String contactFirstName = rs.getString("FIRSTNAME");
    				String contactLastName = rs.getString("LASTNAME");
    				String contactEmail = rs.getString("EMAIL");
    				int addressid = rs.getInt("ID_ADDRESS");
    				String street = rs.getString("STREET");
    				String city = rs.getString("CITY");
    				String zip= rs.getString("ZIP");
    				String country= rs.getString("COUNTRY");
     
    				Contacts.add(new Contact(contactid, contactFirstName, contactLastName, contactEmail, new Address(addressid, street,city,zip,country)));
    			}
    			return Contacts;
    		} catch (NamingException e) {
     
    			return null;
     
    		} catch (SQLException e) {
     
    			return null;
     
    		}
    	}
    Pour le traitement de la méthode displayContacts, je passe par une interface service qui s'occupe d'appeler la méthode getContacts() du DAO.

  4. #4
    Candidat au Club
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Janvier 2018
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Conseil

    Informations forums :
    Inscription : Janvier 2018
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Bon j'ai réussi à trouver la magouille.

    Il suffisait de faire un set de la liste contacts dans la méthode displayContacts en passant en paramètre la liste retournée par la méthode du DAO.
    En d'autres mots :

    Bean Contact
    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
    package bean;
    
    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.annotation.PostConstruct;
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    import javax.naming.NamingException;
    import service.ServiceContact;
    
    @ManagedBean(name = "contact") // Concentré d'actionForm et action
    @SessionScoped
    
    public class Contact implements Serializable {
    	private static final long serialVersionUID = 1L;
    	private int contactId;
    	private String firstName;
    	private String lastName;
    	private String email;
    	private String street;
    	private String city;
    	private String zip;
    	private String country;
    	private Address contactAddress;
    	private List<Contact> contacts;
    
    	public int getContactId() {
    		return contactId;
    	}
    
    	public void setContactId(int contactId) {
    		this.contactId = contactId;
    	}
    
    	public String getFirstName() {
    		return this.firstName;
    	}
    
    	public void setFirstName(String name) {
    		this.firstName = name;
    	}
    
    	public String getLastName() {
    		return this.lastName;
    	}
    
    	public void setLastName(String name) {
    		this.lastName = name;
    	}
    
    	public String getEmail() {
    		return this.email;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	public String getStreet() {
    		return street;
    	}
    
    	public void setStreet(String street) {
    		this.street = street;
    	}
    
    	public String getCity() {
    		return city;
    	}
    
    	public void setCity(String city) {
    		this.city = city;
    	}
    
    	public String getCountry() {
    		return country;
    	}
    
    	public void setCountry(String country) {
    		this.country = country;
    	}
    
    	public String getZip() {
    		return zip;
    	}
    
    	public void setZip(String zip) {
    		this.zip = zip;
    	}
    
    	public Address getContactAddress() {
    		return contactAddress;
    	}
    
    	public void setContactAddress(Address contactAddress) {
    		this.contactAddress = contactAddress;
    	}
    
    	public List<Contact> getContacts() {
    		return contacts;
    	}
    
    	public void setContacts(List<Contact> contacts) {
    		this.contacts = contacts;
    	}
    
    	public Contact() {
    
    	}
    
    	public Contact(int contactId, String firstName, String lastName, String email, Address contactAddress) {
    		this.contactId = contactId;
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.email = email;
    		this.contactAddress = contactAddress;
    	}
    
    	public String checkContact() throws NamingException, SQLException {
    		// Gestion des messages d'erreurs
    		FacesContext context = FacesContext.getCurrentInstance();
    
    		if (isMissing(firstName) || isMissing(lastName) || isMissing(email) || isMissing(street) || isMissing(city)
    				|| isMissing(zip) || isMissing(country)) {
    			if (isMissing(firstName)) {
    				context.addMessage(null, new FacesMessage("Prénom requis"));
    			}
    
    			if (isMissing(lastName)) {
    				context.addMessage(null, new FacesMessage("Nom requis"));
    			}
    
    			if (isMissing(email)) {
    				context.addMessage(null, new FacesMessage("Mail requis"));
    			}
    
    			if (isMissing(street)) {
    				context.addMessage(null, new FacesMessage("Rue requis"));
    			}
    
    			if (isMissing(city)) {
    				context.addMessage(null, new FacesMessage("Ville requis"));
    			}
    
    			if (isMissing(zip)) {
    				context.addMessage(null, new FacesMessage("Code postal requis"));
    			}
    
    			if (isMissing(country)) {
    				context.addMessage(null, new FacesMessage("Pays requis"));
    			}
    
    			return null;
    		} else {
    			final ServiceContact Service = new ServiceContact();
    
    			// Ajout de l'adresse du contact
    			final String addressError = Service.addAddress(street, city, zip, country);
    
    			// Récupère l'identifiant de l'adresse pour pouvoir ajouter
    			// l'adresse au contact
    			final int idAddress = Service.getAddressId(street, city, zip, country);
    
    			// Ajout du contact
    			final String contactError = Service.addContact(firstName, lastName, email, idAddress);
    
    			// Redirection en fonction des erreurs de traitement
    			if (addressError == null || idAddress != 0 || contactError == null) {
    				return ("menu");
    			} else {
    				return ("login");
    			}
    		}
    	}
    
    	@PostConstruct
    	public void initContacts() {
    		final ServiceContact lDAOContact = new ServiceContact();
    		final ArrayList<Contact> Contacts = lDAOContact.getContacts();
    		this.setContacts(Contacts);
    	}
    
    	// Contrôle que les champs sont non-vides
    	private boolean isMissing(String value) {
    		return ((value == null) || (value.trim().isEmpty()));
    	}
    
    }

    Code du 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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    	xmlns:h="http://java.sun.com/jsf/html"
    	xmlns:f="http://java.sun.com/jsf/core">
     
    <h:head>
    	<title>Contact</title>
    </h:head>
     
    <h:body>
    	<h1>Gestion des contacts</h1>
     
    	<h:dataTable value="#{contact.contacts}" var="c">
    		<h:column>
    			<f:facet name="header">ID</f:facet>
    			<h:outputText value="#{c.contactId}"></h:outputText>
    		</h:column>
     
    		<h:column>
    			<f:facet name="header">FIRSTNAME</f:facet>
    			<h:outputText value="#{c.firstName}"></h:outputText>
    		</h:column>
     
    		<h:column>
    			<f:facet name="header">LASTNAME</f:facet>
    			<h:outputText value="#{c.lastName}"></h:outputText>
    		</h:column>
    	</h:dataTable>
    </h:body>
     
    </html>
    En espérant que ça puisse aider d'autres.

    Bonne soirée !

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

Discussions similaires

  1. [LibreOffice][Base de données] Recuperer une liste de tables et une liste de champs d'une table sur LibreOffice & OpenOffice
    Par gerard.sauvage dans le forum OpenOffice & LibreOffice
    Réponses: 2
    Dernier message: 08/04/2014, 12h35
  2. Réponses: 2
    Dernier message: 29/05/2008, 15h59
  3. Tri sur une liste d'objet
    Par Poussy-Puce dans le forum C#
    Réponses: 4
    Dernier message: 12/05/2008, 17h35
  4. afficher une liste d'objets sur une page en flow.
    Par looploop dans le forum JSF
    Réponses: 1
    Dernier message: 18/09/2007, 10h54
  5. [Visual Web] Affichage d'une liste d'objets sur SJSC
    Par Ashen-Shugar dans le forum NetBeans
    Réponses: 4
    Dernier message: 28/05/2006, 20h16

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