Bonjour ,

j'ai un soucis concernant ma jsp , je n'arrive pas à afficher mes erreurs en test de mes champs de formulaire .

Je pense respecter le modèle MVC comme il le faut voici mes différentes class :

1 Class ProduitsForm

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
package fr.isidis.forms;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import fr.isidis.beans.Produits;
 
public class ProduitsForm {
 
	private static  String CHAMP_REF_PRODUIT = "refProduit";
	private static  String CHAMP_NOM_PRODUIT = "nomProduit";
	private static  String CHAMP_PRIX_PRODUIT = "prixProduit";
	private String resultat;
 
	private Map<String, String> erreurs = new HashMap<String, String>();
 
	public Produits produit(HttpServletRequest request ){
		// recuperation des champs du formulaire
		String refProduit = getValeurChamp(request , CHAMP_REF_PRODUIT);
		String nomProduit = getValeurChamp(request, CHAMP_NOM_PRODUIT);
		String prixProduit = getValeurChamp(request, CHAMP_PRIX_PRODUIT);
 
		Produits produit = new Produits();
 
		try {
			validationRefProduit(CHAMP_REF_PRODUIT);
		} catch (Exception e) {
			setErreur(CHAMP_REF_PRODUIT, e.getMessage());
		}produit.setRefProduit(refProduit);
 
 
	/*	try {
			validationNomProduit(CHAMP_NOM_PRODUIT);
		} catch (Exception e) {
			setErreur(CHAMP_NOM_PRODUIT, e.getMessage());
		}*/produit.setNomProduit(nomProduit);
 
 
	/*	try {
			validationPrixProduit(CHAMP_PRIX_PRODUIT);
		} catch (Exception e) {
			setErreur(CHAMP_PRIX_PRODUIT, e.getMessage());
		}*/produit.setPrixProduit(prixProduit);
 
 
 
 
		/* Initialisation du résultat global de la validation. */     
		if ( erreurs.isEmpty() ) {        
			setResultat("Succès de l'enregistrement.");   
		} else {   
			setResultat("Échec de l'enregistrement.");     
		}         return produit;  
 
	}    
 
 
 
	//methode de verification de la ref produit
	public void validationRefProduit(String refProduit)throws Exception{
		if(refProduit == null){
 
				throw new Exception("Veuillez donner une ref au produit");
			}
		}
 
 
 
 
	/*//methode de verification du nom de produit
	public void validationNomProduit(String nomProduit)throws Exception{
		if(nomProduit == null){
 
				throw new Exception("Veuillez donner un nom au produit");
 
		}
	}
 
	//methode de verification du prix du produit
	public void validationPrixProduit(String prixProduit)throws Exception{
		if(prixProduit == null){
			throw new Exception("Veuillez donner un prix au produit");
		}
	}
 
*/
 
	public Map<String, String> getErreurs() { 
		return erreurs;   
	}     
 
	/*     * Ajoute un message correspondant au champ spécifié à la map des erreurs.     */   
	private void setErreur( String champ, String message ) {    
		erreurs.put( champ, message );    }  
 
 
 
	/*     * Méthode utilitaire qui retourne null si un champ est vide, et son contenu     * sinon.     */ 
	private static String getValeurChamp( HttpServletRequest request, String nomChamp ) {   
		String valeur = request.getParameter( nomChamp );       
		if ( valeur == null || valeur.trim().length() == 0 ) { 
			return null;      
		} else {        
			return valeur;       
		}   
	}
 
 
 
	public String getResultat() {
		return resultat;
	}
 
 
 
	public void setResultat(String resultat) {
		this.resultat = resultat;
	}
}

2 . Class Produits

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
package fr.isidis.beans;
 
public class Produits {
	private String refProduit;
	private String nomProduit;
	private String prixProduit;
 
	public String getRefProduit() {
		return refProduit;
	}
	public void setRefProduit(String refProduit) {
		this.refProduit = refProduit;
	}
	public String getNomProduit() {
		return nomProduit;
	}
	public void setNomProduit(String nomProduit) {
		this.nomProduit = nomProduit;
	}
	public String getPrixProduit() {
		return prixProduit;
	}
	public void setPrixProduit(String prixProduit) {
		this.prixProduit = prixProduit;
	}
 
}
3. Ma servlet GestionProduits

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
package fr.isidis.servlets;
 
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import fr.isidis.beans.Produits;
import fr.isidis.forms.ProduitsForm;
 
/**
 * Servlet implementation class GestionProduits
 */
public class GestionProduits extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String ATT_USER = "utilisateur";
	public static final String ATT_FORM = "form";
	public static final String ATT_PRODUIT = "produit";
	public static final String ATT_SESSION_USER = "sessionUtilisateur";  
	public static final String VUE = "/WEB-INF/gestionProduit.jsp";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GestionProduits() {
        super();
        // TODO Auto-generated constructor stub
    }
 
	/**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.getServletContext().getRequestDispatcher( VUE ).forward( request, response ); 
	}
 
	/**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.getAttribute(ATT_USER);
		//preparation de l'objet formulaire gestion produit
		ProduitsForm formProduits = new ProduitsForm();
		//Appel au traitement et à la validation de la requête et récupération du bean en résultant
		Produits Produit = formProduits.produit(request);
		//stockage du formulaire et du bean dans l'objet request
		request.setAttribute(ATT_FORM, formProduits);
		request.setAttribute(ATT_PRODUIT, Produit);
 
		this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
	}
 
}
4 . Ma jsp gestionProduit.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
34
35
36
37
38
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" rel="stylesheet" href="public/form.css" />
<title>Gestion des produits</title>
</head>
<body>
<form name="gestionProduit" method="post" action="<c:url value="/gestionProduits"/>">
<fieldset>
<legend>Gestion des produits</legend>
<br>
 
<label for="refProduit">Référence de l'article : <span class="requis">*</span></label>
<input type="text" id="refProduit" name="refProduit" value="<c:out value="${produit.refProduit}"/>" size="10" maxlength="20" />
 <span class="erreur">${formProduits.erreurs['refProduit']}</span>
 
<br>
 
<label for="nomProduit">Nom de l'article : <span class="requis">*</span></label>
<input type="text" id="nomProduit" name="nomProduit" value="<c:out value="${produit.nomProduit}"/>" size="10" maxlength="20" />
 <span class="erreur">${formProduits.erreurs['nomProduit']}</span>
 
<br>
 
<label for="prixProduit">Prix de l'article : <span class="requis">*</span></label>
<input type="text" id="prixProduit" name="prixProduit" value="<c:out value="${produit.prixProduit}"/>" size="10" maxlength="20" />
 <span class="erreur">${formProduits.erreurs['prixProduit']}</span>
 
<input type="submit" name="boutonEnregistrer" value="Enregistrer"/>
<p class="${empty formProduits.erreurs ? 'succes' : 'erreur'}">${formProduits.resultat}</p>
 
</fieldset>
</form>
</body>
</html>
J'utilise la library jstl , mon formulaire n'affiche pas les erreurs et test de vérifications je voudrais que par exemple quand mon champs refProduit est vide et qu'on clique sur le bouton enregistrer celui ci me met l'exception associé qui est de veuillez remplir le champs refProduit ...

Merci d'avance