Spring MVC: Vente :Liste Ventes et Facture
Les étapes à suivre
- 1Redéfinition des méthodes génériques dans VenteRepository.java
- 2Créer la méthode listeVente() GET dans le controller VenteController.java qui permet d'afficher le contenu de la table vente et l'afficher dans la vue ventes/liste.jsp
- 3Créer la vue ventes/liste.jsp pour afficher la liste des ventes
- 4Créer la méthode facture(idUser) GET dans le controller VenteController.java qui permet d'afficher d'afficher la facture d'un utilisateur dont id est passé en paramètre pui et l'afficher dans la vue ventes/facture.jsp
- 5Créer la vue ventes/facture.jsp pour afficher et imprimer la facture envoyé par la méthode facture(idUser)
1.Redéfinition des méthodes génériques dans VenteRepository.java
package com.ecomerce.DAO;
import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import com.ecomerce.models.Produit;
import com.ecomerce.models.Utilisateur;
import com.ecomerce.models.Vente;
public interface VenteRepository extends CrudRepository<Vente,Integer> {
/*findByClientId(iduser):va selectionner les lignes de la table vente
dont leur idClient=iduser
*/
List<Vente> findByClientId(int iduser);
/*On peut Créer nos propre méthode avec @Query
@Query:permet de créer des requêtes SQL personnalisés
:u et la valeur du paramètre de la méthode findByClient */
@Query("select v from Vente v where v.client= :u")
List<Vente> findByClient( @Param("u") Utilisateur u);
/*
L'entité Vente a les attributs suivants:
int id, Date dateVente, float quantite, Produit produit, Utilisateur client
donc VenteRepository contient les méthodes génériques suivantes:
*/
/*select * from vente where id=id*/
List<Vente> findById(int id);
/*select * from vente where datevente=dateVente*/
List<Vente> findBydateVente(Date dateVente);
/*select * from vente where quantite=quantite*/
List<Vente> findByquantite(float quantite);
/*select * from vente where Produit_Id=produit.id*/
List<Vente> findByProduit(Produit produit);
/*select * from vente where Produit_Id=idproduit*/
List<Vente> findByProduitId(int idproduit);
/*select * from vente v,produit p where v.produit_id=p.id and p.nom=nom*/
List<Vente> findByProduitNom(String nom);
//...
}
2.Créer la méthode listeVente() GET dans le controller VenteController.java
//afficher la liste des ventes
@RequestMapping(value="/liste")
public String listeVente(Model m)
{
/*findAll() une méthode générique de CrudRepository ,elle retourne un Iterable
donc on doit le convertir à une list pour l'envoyer à la vue liste.jsp */
Iterable<Vente> listeVente=venteRepository.findAll();
List<Vente> listeventes = new ArrayList<Vente>();
listeVente.forEach(listeventes::add);
/* Chaque objet listeventes est de type vente donc il contient:
{
id,datevente,qte,
utilisateur:{id,nom,prenom,email,age,photo},
produit:{id,nom,prix,marque,qteStok}
}
Afin d'afficher les information du produit ou l'utilisateur dans la vue liste.jsp on écrit:
foreach(listevente as o)
{
o.utilisateur.id retourne id de l'utilisateur
o.utilisateur.nom ==>le nom
o.produit.prix ==>le prix du produit
...
}
Remarque:les setters et les getters dans les classes :Produit ,Utilisateur est Vente
Sont obligatoires
*/
/*Envoyer la liste des ventes à la vue liste.jsp */
m.addAttribute("listevente",listeventes);
return "ventes/liste";
}
3.Créer la vue ventes/liste.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html><head><title>Liste produits</title></head>
<body><style>
table,thead,tbody,tr{width:100%;} thead{background:#ddd;} td{border:1px solid #000;}
a {margin-right:10px;}</style>
<span>Liste Ventes</span>
<table>
<thead><th>ID</th><th>Date</th><th>Client</th><th>Produit</th><th>Quantite</th><th>Prix</th><th>Totale</th>
</thead>
<tbdoy>
<!-- Déclaration d'un variable totale et initialiser à 0 En utilisatant les tag de JSTL-->
<c:set var = "totale" scope = "session" value = "0"/>
<c:forEach items="${listevente}" var="v">
<tr>
<td>${v.id}</td>
<td>${v.dateVente}</td>
<td>${v.client.nom}</td>
<td>${v.produit.nom}</td>
<td>${v.quantite}</td>
<td>${v.produit.prix}</td>
<td>${v.quantite*v.produit.prix}</td>
<td><a href="${pageContext.request.contextPath}/ventes/${v.client.id}/facture">Facture</a></td>
<!--incrémenter le totale des vente par totale+(v.quantite*v.produit.prix)-->
<c:set var = "totale" scope = "session" value = "${totale+(v.quantite*v.produit.prix)}"/>
</tr>
</c:forEach>
</tbdoy>
</table>
${totale}
</body>
</html>
4.Créer la méthodefacture(idUser) GET dans le controller VenteController.java
//Afficher la facture d'un client dont l'id est passé en paramètre
@RequestMapping(value="/{iduser}/facture")
public String facture(@PathVariable int iduser,Model m)
{
/*findByClientId(int id) :une méthode générique de CrudRepository on doit
définir la signature de cette méthode dans VenteRepository */
List<Vente> listevente=venteRepository.findByClientId(iduser);
m.addAttribute("listevente",listevente);
return "ventes/facture";
}
5.Créer la vue ventes/facture.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html><head><title>Facture du client ${listevente[0].client.nom}</title>
</head>
<body>
<style>
table,thead,tbody,tr{width:100%;} thead{background:#ddd;} td{border:1px solid #000;}
@media print{button{display:none;}}
a {margin-right:10px;}</style>
<h3>Les informations du Client:</h3>
<table>
<tr><td>Facture num :<b>${listevente[0].id}</b></td></tr>
<tr><td>Nom :<b>${listevente[0].client.nom}</b></td></tr>
<tr><td>Prenom:<b>${listevente[0].client.prenom}</b></td></tr>
<tr><td>Email :<b>${listevente[0].client.email}</b></td></tr>
</table>
<h3>Les Commandes:</h3>
<table>
<thead><th>ID</th><th>Date</th><th>Client</th><th>Produit</th><th>Quantite</th><th>Prix</th><th>Totale</th>
</thead><tbdoy><c:set var = "totale" scope = "session" value = "0"/>
<c:forEach items="${listevente}" var="v">
<tr>
<td>${v.id}</td>
<td>${v.dateVente}</td>
<td>${v.client.nom}</td>
<td>${v.produit.nom}</td>
<td>${v.quantite}</td>
<td>${v.produit.prix}</td>
<td>${v.quantite*v.produit.prix}</td>
<c:set var = "totale" scope = "session" value = "${totale+(v.quantite*v.produit.prix)}"/>
</tr>
</c:forEach>
</tbdoy>
</table>
<div style="background:#ddd;">Totale à paye: <b>${totale}</b></div>
<br/><br/><button onclick="window.print();">Imprimer Facture</button>
</body>
</html>