Skip to content

Commit

Permalink
javaee-projets-movie-db - recherche de produits
Browse files Browse the repository at this point in the history
  • Loading branch information
ltearno committed Nov 10, 2017
1 parent ad2c9a6 commit 0d5ab62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
import fr.lteconsulting.training.moviedb.model.Produit;

import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import java.util.List;

@Stateless
public class GestionProduits extends GestionGenerique<Produit> {
public GestionProduits() {
super(Produit.class);
}

public List<Produit> findByName(String search) {
TypedQuery<Produit> query = em.createQuery("select p from Produit p where p.nom like :search", Produit.class);
query.setParameter("search", "%" + search + "%");
return query.getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ public class ProduitsServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Produit> produits = gestionProduits.findAll();
List<Produit> produits;

String search = req.getParameter("search");
if (search != null && !search.isEmpty())
produits = gestionProduits.findByName(search);
else
produits = gestionProduits.findAll();

Vues.afficherProduits(req, resp, produits);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
List<Produit> produits = (List<Produit>) request.getAttribute("produits");
%>

<h1>Recherche</h1>
<form>
<label>Recherche dans les produits : <input type="text" name="search"></label>
<button>Cherche</button>
</form>

<h1>Produits</h1>
<table>
<tr>
Expand Down

0 comments on commit 0d5ab62

Please sign in to comment.