Bonjour à la communauté,
Je suis confronté à un problème d'affichage de données; je m'explique.
Je fait du java EE (servlets, jsp, taglib). J'arrive à créer des enregistrement dans ma base de données. Mon Problème est de savoir comment restituer ces données dans un tableau contenu dans une page JSP
J'ai fait un code à cet effet mais malheureusement il me retourne une seule ligne (j'ai cherché le bug en vain )
voici comment je procède:
Mon interface Profils (profils est le nom de ma table)
l'implémentation de mon interface Profils:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7... public interface ProfilDao { //Pour lister les profils List<Profils> listerProfils() throws DAOException; }
Beans du Profils
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 public class ProfilsDaoImpl implements ProfilDao { private DAOFactory daoFactory; private static final String SQL_SELECT_PROFIL = "" + "SELECT idprofil, libelleprofil, actif " + "FROM vprofils " + "ORDER BY libelleprofil DESC; "; /*Constructeur*/ ProfilsDaoImpl( DAOFactory daoFactory ) { this.daoFactory = daoFactory; } * Implémentation de la méthode définie dans l'interface ProfilsDao */ @Override public List<Profils> listerProfils() throws DAOException { Connection connection = null; PreparedStatement STMT = null; ResultSet RST = null; List<Profils> Profils = new ArrayList<Profils>(); try { connection = daoFactory.getConnection(); STMT = connection.prepareStatement(SQL_SELECT_PROFIL ); RST = STMT.executeQuery(); while ( RST.next() ) { Profils.add( map( RST ) ); } } catch ( SQLException e ) { throw new DAOException( e ); } finally { fermeturesSilencieuses( RST, STMT,connection ); } return Profils; } /* Mapping : correspondance entre le beans et le schema relationnel de la base de donnees */ private static Profils map( ResultSet rs ) throws SQLException { Profils Profils = new Profils(); Profils.setIDrofil(rs.getInt("idprofil")); Profils.setLibelleProfil(rs.getString( "libelleprofil" )); Profils.setEtatProfil(rs.getBoolean("actif")); return Profils; } }
Pour l'affichage, j'ai crée un filtre qui precharge les données dans une map
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 public class Profils { private static long idProfil; private static String libelleProfil; private static Boolean etatProfil; public void setIDrofil(long id){ this.idProfil=id; } public static long getIDrofil(){ return idProfil; } public void setLibelleProfil(String profilname){ this.libelleProfil=profilname; } public static String getLibelleProfil(){ return libelleProfil; } public void setEtatProfil(Boolean profilStatut){ this.etatProfil=profilStatut; } public static Boolean getEtatProfil(){ return etatProfil; } }
public void doFilter( ServletRequest req, ServletResponse res,FilterChain chain ) throws IOException,ServletException {
HttpServletRequest request = (HttpServletRequest) req;
/* Récupération de la session depuis la requête */
HttpSession session = request.getSession();
if ( session.getAttribute( ATT_SESSION_PROFILS ) == null ){
List<Profils> listeProfils = ProfilDao.listerProfils();
Map<Long, Profils> mapProfils = new HashMap<Long,Profils>();
for ( Profils profils : listeProfils) {
mapProfils.put( profils.getIDrofil(), profils );
}
session.setAttribute( ATT_SESSION_PROFILS, mapProfils);
}
chain.doFilter( request, res );
}
Et enfin l'affichage de ma page jsp (qui est appélée via une servlet
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 ... <table style="width:100%;font-family:Times New Roman; font-size:12px; border-collapse:collapse " > <tr> <th class="formatCelluleEntete" scope="col" style=" background-color:#CFF5FC; ">N°</th> <th class="formatCelluleEntete" scope="col">Libellé</th> <th class="formatCelluleEntete" scope="col">Etat</th> </tr> <c:forEach items="${ sessionScope.profils }" var="mapProfils" varStatus="boucle"> <tr class="${boucle.index % 2 == 0 ? 'pair' :'impair'}"> <td class="formatCelluleDonnees" width="40"><c:out value="${ mapProfils.value.getIDrofil()}"/> </td> <td class="formatCelluleDonnees" ><c:out value="${ mapProfils.value.getLibelleProfil()}"/></td> <td class="formatCelluleDonnees" ><c:out value="${ mapProfils.value.getEtatProfil()}"/></td> </tr> </c:forEach> </table> ...
D'avance merci.
Partager