Bonjour ,
Voila j'ai 2 form celui du ClientForm et de la CommandeForm que j'avais déja crée un beans pour les 2 , quand j’accède de ma servlet "CommandeInscription" pour afficher les erreurs devant chaque champ de mon formulaire , les erreurs des champs Commande (montant,mode livraison ..) s'affiche correctement mais ceux du Client ne s'affiche pas pourtant j'ai bien mis le getErreur du ClientForm dans la map des erreurs du CommandeForm ,voila mes codes :
Code beans Client :
Code bean Commande :
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 package com.commande.beans; public class Client { private long id_client; private String nom; private String prenom; private String adresse_livraison; private String telephone; private String email; public Client() { super(); } public long getId_client() { return id_client; } public String getNom() { return nom; } public String getPrenom() { return prenom; } public String getAdresse_livraison() { return adresse_livraison; } public String getTelephone() { return telephone; } public String getEmail() { return email; } public void setId_client( long id_client ) { this.id_client = id_client; } public void setNom( String nom ) { this.nom = nom; } public void setPrenom( String prenom ) { this.prenom = prenom; } public void setAdresse_livraison( String adresse_livraison ) { this.adresse_livraison = adresse_livraison; } public void setTelephone( String telephone ) { this.telephone = telephone; } public void setEmail( String email ) { this.email = email; } }
code du ClientForm :
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 package com.commande.beans; public class Commande { private long id_commande; private long id_client; private String date_commande; private String montant; private String mode_payement; private String status_payement; private String mode_livraison; private String status_livraison; private Client client; public Commande() { super(); } public Client getClient() { return client; } public void setClient( Client client ) { this.client = client; } public long getId_commande() { return id_commande; } public long getId_client() { return id_client; } public String getDate_commande() { return date_commande; } public String getMontant() { return montant; } public String getMode_payement() { return mode_payement; } public String getStatus_payement() { return status_payement; } public String getMode_livraison() { return mode_livraison; } public String getStatus_livraison() { return status_livraison; } public void setId_commande( long id_commande ) { this.id_commande = id_commande; } public void setId_client( long id_client ) { this.id_client = id_client; } public void setDate_commande( String date_commande ) { this.date_commande = date_commande; } public void setMontant( String montant ) { this.montant = montant; } public void setMode_payement( String mode_payement ) { this.mode_payement = mode_payement; } public void setStatus_payement( String status_payement ) { this.status_payement = status_payement; } public void setMode_livraison( String mode_livraison ) { this.mode_livraison = mode_livraison; } public void setStatus_livraison( String status_livraison ) { this.status_livraison = status_livraison; } }
Code CommandeForm :
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public class ClientForm { // Declarations : private String champ_nomClient = "nomClient"; private String champ_prenomClient = "prenomClient"; private String champ_adresseClient = "adresseLivraisonClient"; private String champ_telephoneClient = "telephoneClient"; private String champ_adresseEmail = "adresseClient"; public Map<String, String> erreur = new HashMap<String, String>(); public String resultat; public Map<String, String> getErreur() { return erreur; } public String getResultat() { return resultat; } // methode de la form : public Client FormClient( HttpServletRequest request ) { String nomClient = getValueChamp( request, champ_nomClient ); String prenomClient = getValueChamp( request, champ_prenomClient ); String adresseClient = getValueChamp( request, champ_adresseClient ); String telephoneClient = getValueChamp( request, champ_telephoneClient ); String adresseEmail = getValueChamp( request, champ_adresseEmail ); Client client = new Client(); traiterNomClient( nomClient, client ); traiterPrenomClient( prenomClient, client ); traiterAdresseClient( adresseClient, client ); traiterTelephoneClient( telephoneClient, client ); traiterAdresseEmail( adresseEmail, client ); if ( erreur.isEmpty() ) { resultat = "Inscription avec succès"; } else { resultat = "Inscription échoué"; } return client; } // Methodes utilitaires : public void traiterAdresseEmail( String adresseemail, Client client ) { try { validationAdresseEmail( adresseemail ); } catch ( FormException e ) { setErreur( adresseemail, e.getMessage() ); } client.setEmail( adresseemail ); } public void traiterTelephoneClient( String telephoneclient, Client client ) { try { validationTelephoneClient( telephoneclient ); } catch ( FormException e ) { setErreur( telephoneclient, e.getMessage() ); } client.setTelephone( telephoneclient ); } public void traiterAdresseClient( String adresseclient, Client client ) { try { validationAdresseClient( adresseclient ); } catch ( FormException e ) { setErreur( adresseclient, e.getMessage() ); } client.setAdresse_livraison( adresseclient ); } public void traiterPrenomClient( String prenomclient, Client client ) { try { validationPrenomClient( prenomclient ); } catch ( FormException e ) { setErreur( prenomclient, e.getMessage() ); } client.setPrenom( prenomclient ); } public void traiterNomClient( String nomclient, Client client ) { try { validationNomClient( nomclient ); } catch ( FormException e ) { setErreur( nomclient, e.getMessage() ); } client.setNom( nomclient ); } public void validationAdresseEmail( String adresseEmail ) throws FormException { if ( adresseEmail != null ) { if ( !adresseEmail.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) { throw new FormException( "L'adresse email du client n'est pas valide" ); } } else { throw new FormException( "Veuilez entrer l'adresse email du client" ); } } public void validationTelephoneClient( String telephoneClient ) throws FormException { if ( telephoneClient != null ) { if ( !telephoneClient.matches( "^\\d+$" ) ) { throw new FormException( "Le numéro de téléphone doit etre numérique" ); } else if ( !( telephoneClient.trim().length() == 10 ) ) { throw new FormException( "Le numéro de téléphone doit contenir 10 chiffres" ); } } else { throw new FormException( "Veuillez entrer le numéro de téléphone du client" ); } } public void validationAdresseClient( String adresseClient ) throws FormException { if ( adresseClient != null ) { if ( adresseClient.trim().length() < 5 ) { throw new FormException( "L'adresse du client doit contenir plus de 5 caractères" ); } } else { throw new FormException( "Veuilez entrer l'adresse du client" ); } } public void validationPrenomClient( String prenom ) throws FormException { if ( prenom != null ) { if ( prenom.trim().length() < 3 ) { throw new FormException( "Le prenom du client doit contenir plus de 3 caractères" ); } } else { throw new FormException( "Veuilez entrer le prenom du client" ); } } public void validationNomClient( String nom ) throws FormException { if ( nom != null ) { if ( nom.trim().length() < 3 ) { throw new FormException( "Le nom du client doit contenir plus de 3 caractères" ); } } else { throw new FormException( "Veuilez entrer le nom du client" ); } } public void setErreur( String champ, String message ) { erreur.put( champ, message ); } public String getValueChamp( HttpServletRequest request, String champ ) { String value = request.getParameter( champ ); if ( value == null || value.trim().length() == 0 ) { return null; } else { return value.trim(); } } }
Code de la 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public class CommandeForm { private String champ_montant = "montant"; private String champ_modePayement = "modePayement"; private String champ_statusPayement = "statusPayement"; private String champ_modeLivraison = "modeLivraison"; private String champ_statusLivraison = "statusLivraison"; private String format_date = "DD/MM/YYYY HH:MM"; public Map<String, String> erreurs = new HashMap<String, String>(); public String resultat; public Map<String, String> getErreurs() { return erreurs; } public String getResultat() { return resultat; } // methode de la form : public Commande FormCommande( HttpServletRequest request ) { String montant = getValueChamp( request, champ_montant ); String modePayement = getValueChamp( request, champ_modePayement ); String statusPayement = getValueChamp( request, champ_statusPayement ); String modeLivraison = getValueChamp( request, champ_modeLivraison ); String statusLivraison = getValueChamp( request, champ_statusLivraison ); // Form client : ClientForm form_client = new ClientForm(); Client client = form_client.FormClient( request ); erreurs = form_client.getErreur(); Commande commande = new Commande(); commande.setClient( client ); // //////////////////////*****//////////////////// DateTime dt = new DateTime(); DateTimeFormatter forma = DateTimeFormat.forPattern( format_date ); String dateLivraison = dt.toString( forma ); commande.setDate_commande( dateLivraison ); traiterMontant( montant, commande ); traiterModePayement( modePayement, commande ); traiterStatusPayement( statusPayement, commande ); traiterModeLivraison( modeLivraison, commande ); traiterStatusLivraison( statusLivraison, commande ); if ( erreurs.isEmpty() ) { resultat = "Inscription avec succès"; } else { resultat = "Inscription échoué"; } return commande; } // les methodes utilitaires : public void traiterStatusLivraison( String statusLivraison, Commande commande ) { try { validationStatusLivraison( statusLivraison ); } catch ( FormException e ) { setErreurs( champ_statusLivraison, e.getMessage() ); } commande.setStatus_livraison( statusLivraison ); } public void traiterModeLivraison( String modeLivraison, Commande commande ) { try { validationModeLivraison( modeLivraison ); } catch ( FormException e ) { setErreurs( champ_modeLivraison, e.getMessage() ); } commande.setMode_livraison( modeLivraison ); } public void traiterStatusPayement( String statusPayement, Commande commande ) { try { validationStatusPayement( statusPayement ); } catch ( FormException e ) { setErreurs( champ_statusPayement, e.getMessage() ); } commande.setStatus_payement( statusPayement ); } public void traiterModePayement( String modePayement, Commande commande ) { try { validationModePayement( modePayement ); } catch ( FormException e ) { setErreurs( champ_modePayement, e.getMessage() ); } commande.setMode_payement( modePayement ); } public void traiterMontant( String montant, Commande commande ) { try { validationMontant( montant ); } catch ( FormException e ) { setErreurs( champ_montant, e.getMessage() ); } commande.setMontant( montant ); } public void validationStatusLivraison( String statusLivraison ) throws FormException { if ( statusLivraison == null ) { throw new FormException( "Veuillez entrer la status de livraison" ); } } public void validationModeLivraison( String modeLivraison ) throws FormException { if ( modeLivraison == null ) { throw new FormException( "Veuillez entrer le mode de livraison" ); } } public void validationStatusPayement( String statusPayement ) throws FormException { if ( statusPayement == null ) { throw new FormException( "Veuillez entrer la status de payement" ); } } public void validationModePayement( String modePayement ) throws FormException { if ( modePayement == null ) { throw new FormException( "Veuillez entrer le mode de payement" ); } } public double validationMontant( String montant ) throws FormException { double valeur; if ( montant != null ) { try { valeur = Double.parseDouble( montant ); if ( valeur < 0 ) { throw new FormException( "Le montant doit etre positive " ); } } catch ( NumberFormatException e ) { valeur = -1; throw new FormException( "Le montant doit etre un nombre numérique" ); } } else { valeur = -1; throw new FormException( "Veuillez entrer un montant" ); } return valeur; } public void setErreurs( String champ, String message ) { erreurs.put( champ, message ); } public String getValueChamp( HttpServletRequest request, String champ ) { String value = request.getParameter( champ ); if ( value == null || value.trim().length() == 0 ) { return null; } else { return value.trim(); } } }
Code du formulaire :
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 public class CommandeInscription extends HttpServlet { private static final long serialVersionUID = 1L; private static final String vue_formulaireCommande = "/WEB-INF/formulaire_commande.jsp"; private static final String Form_Commande = "formCommande"; private static final String Bean_commande = "beanCommande"; /** * @see HttpServlet#HttpServlet() */ public CommandeInscription() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init() throws ServletException { // TODO Auto-generated method stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { this.getServletContext().getRequestDispatcher( vue_formulaireCommande ).forward( request, response ); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { CommandeForm form_commande = new CommandeForm(); Commande commande = form_commande.FormCommande( request ); request.setAttribute( Form_Commande, form_commande ); request.setAttribute( Bean_commande, commande ); this.getServletContext().getRequestDispatcher( vue_formulaireCommande ).forward( request, response ); } }
Photo du problème :
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 <div id="formulaireCommande"> <fieldset> <span class="rouge">*</span> Champ obligatoire . <legend>Formulaire de la commande</legend> <form action="<c:out value="CommandeInscription"/>" method="post"> <p>Nom du client: <input type="text" value="<c:out value="${beanCommande.client.nom}"/>" name="nomClient"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.nomClient}"></c:out> </p> <p>Prénom du client : <input type="text" value="<c:out value="${beanCommande.client.prenom}"/>" name="prenomClient"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.prenomClient}"></c:out> </p> <p>Adresse de livraison : <textarea cols="25" rows="3" name="adresseLivraisonClient"></textarea><span class="rouge">*</span> <c:out value="${formCommande.erreurs.adresseLivraisonClient}"></c:out> </p> <p>Numéro de téléphone du client: <input type="text" value="<c:out value="${beanCommande.client.telephone}"/>" name="telephoneClient"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.telephoneClient}"></c:out> </p> <p>Adresse email du client: <input type="text" value="<c:out value="${beanCommande.client.email}"/>" name="adresseClient"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.adresseClient}"></c:out> </p> <p>Date de la commande : <input type="text" value="<c:out value="${beanCommande.date_commande}"/>" name="dateCommande" disabled="disabled"/></p> <p>Montant : <input type="text" value="" name="montant"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.montant}"></c:out> </p> <p>Mode de payement : <input type="text" value="" name="modePayement"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.modePayement}"></c:out> </p> <p>Status de payement : <input type="text" value="" name="statusPayement"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.statusPayement}"></c:out> </p> <p>Mode de livraison : <input type="text" value="" name="modeLivraison"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.modeLivraison}"></c:out> </p> <p>Status de livraison : <input type="text" value="" name="statusLivraison"/><span class="rouge">*</span> <c:out value="${formCommande.erreurs.statusLivraison}"></c:out> </p> <p><input type="submit" value="Valider la commande"/> <input type="reset" value="Remettre a zero"/></p> <span><c:out value="${formCommande.resultat}"></c:out></span> </form> </fieldset> </div> <div id="ecriture"> <p>Ce ci est un projet assez complet de ce que j'ai appris dans le tuto de Coyote celui de créer une application JEE en suivant le modele MVC (modèle , vue , controleur) . </p> </div>
Partager