Bonjour,
Je suis débutant en java.
J'essaye d'afficher une liste de produit dans ma page jsp et ce, depuis mon servlet.
Mon code est le suivant..
J'ai crée une class Product :
Mon servlet est le suivant :
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 public class Product { private int productId; private String id_cate; private String id_Type; private String cpu; private String ram; private String hdd; private String bandwith; private double price; public Product(int productId, String id_cate, String id_Type, String cpu, String ram, String hdd, String bandwith, double price) { this.productId = productId; this.id_cate = id_cate; this.id_Type = id_Type; this.cpu = cpu; this.ram = ram; this.hdd = hdd; this.bandwith = bandwith; this.price = price; } + get /set
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 // Initialisation de la liste des produits ArrayList<Product> products = new ArrayList(); // Creation d'une liste de produits products.add(new Product(1, "Mutualise", "Luxe", "Core i5", "2go ram", "500go", "2mo/s", 12)); products.add(new Product(2, "Dedie", "Normal", "Core i7", "4go ram", "1to", "1mo/s", 10)); products.add(new Product(3, "Dedie", "Mini", "Core i3", "2go ram", "500go", "1.5mo/s", 25)); products.add(new Product(4, "Mutualise", "Normal", "Core i5", "2go ram", "750go", "2mo/s", 5)); products.add(new Product(5, "Mutualise", "Mini", "Core i3", "2go ram", "500go", "2mo/s", 98)); products.add(new Product(6, "Dedie", "Luxe", "Core i7", "4go ram", "500go", "2mo/s", 59)); // Utiliser product dans la view Index ? request.setAttribute("products", products); RequestDispatcher view = request.getRequestDispatcher("index.jsp"); view.forward(request, response);
Pouvez-vous me dire si je suis dans le bon ?
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 <%! ArrayList<Product> products; products = (ArrayList<Product>) request.getAttribute("products"); %> <h3>Liste de produits</h3></br> </br> <!-- boucle => un tableau de chaque "Type" --> <% for (Product p : products) {%> <table class="table"> <thead> <tr> <th class="hidden">Id</th> <th>Categorie</th> <th>Type</th> <th>RAM</th> <th>CPU</th> <th>HardDisk</th> <th>BandWith</th> <th>Prix</th> </tr> </thead> <tbody> <tr> <td class="hidden"> p.getId</td> <td><%= p.getId_cate() %> </td> <td><%= p.getId_Type() %> </td> <td><%= p.getRam() %></td> <td><%= p.getCpu() %></td> <td><%= p.getHdd() %></td> <td><%= p.getBandwith() %></td> <td><%= p.getPrice() %></td> </tr> </tbody> </table>
Merci beaucoup de votre aide !
Bien à vous,
Arsenik
Partager