Bonjour,
Je suis entrain de réaliser une application en utlisant spring thymeleaf mais après avoir mis dans mes codes, les instructions de validation du formulaire mais lorsque je clique pour tester le champs obligatoire le message suivante s'affiche:Pièce jointe 366357
Voici le code du controlleur:
Code:
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 package com.wangi.aeropro.web; import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.wangi.aeropro.dao.PersonnelAeronautiqueRepository; import com.wangi.aeropro.entites.PersonnelAeronautique; @Controller @RequestMapping(value="/personnels") public class PersonnelAeronautiqueController { //besoin de notre interface @Autowired public PersonnelAeronautiqueRepository personnelRepository; //création des méthodes @RequestMapping(value="/index") public String Index(Model model, @RequestParam(name="page",defaultValue="0")int p, @RequestParam(name="motcle",defaultValue="")String mc) { //cette vue doit affiher une liste des personnels Page<PersonnelAeronautique>pers=personnelRepository.chercherPersonnel("%"+mc+"%", new PageRequest(p,10));//nombre d'element par page int pagesCount=pers.getTotalPages(); int []pages=new int[pagesCount]; for(int i=0;i<pagesCount;i++)pages[i]= i; model.addAttribute("pages",pages); model.addAttribute("pagePersonnels",pers); model.addAttribute("pageCourante",p);//p du paramétre RequestMapping qui veut dire pageCourante model.addAttribute("motCle",mc);//notre motcle via mc return "personnels"; } //formulaire de saisie //la méthode get pcq pour avoir le formulaire on passe d'abord par Get pour que //la page s'affiche @RequestMapping(value="/formPersonnelAeronautique",method=RequestMethod.GET) public String formPersonnelAeronautique(Model model) { model.addAttribute("personnelAe", new PersonnelAeronautique()); return "formPersonnelAeronautique"; } //action savePersonnel //Nous ajoutons la validation coté serveur en cas d'erreur //on stock l'erreur dans un objet ByndingResult @RequestMapping(value="/savePersonnel",method=RequestMethod.POST) public String SavePersonnel(@Valid PersonnelAeronautique pe, BindingResult bindingResult) { if(bindingResult.hasErrors()) { //encas des erreurs on reviens vers le formulaire return "formPersonnelAeronautique"; } personnelRepository.save(pe); //en suite on fait une rediction vers index dans le meme controlleur return "redirect:index"; } }
L'entité:
La vue:Code:
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 package com.wangi.aeropro.entites; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.Size; import org.springframework.format.annotation.DateTimeFormat; @Entity public class PersonnelAeronautique implements Serializable{ @Id @GeneratedValue private Long idperaero; @NotEmpty @Size(min=5,max=25) @Column(name="nompe",length=25) private String nompe; @Column(name="prenom",length=15) private String prenom; @NotEmpty @Column(name="residence",length=50) private String residence; @DateTimeFormat(pattern ="yyyy-MM-dd") private Date datenais; @NotEmpty @Column(name="lieunais",length=15) private String lieunais; @Column(name="nationalite",length=25) private String nationalite; @NotEmpty @Column(name="numtel",length=15) private String numtel; @Column(name="email",length=25) private String email; @Column(name="codecat",length=15) private String codecat; @Column(name="lingustique",length=25) private String lingustique; private String photo; public PersonnelAeronautique() { super(); // TODO Auto-generated constructor stub } public PersonnelAeronautique(Long idperaero, String nompe, String prenom, String residence, Date datenais, String lieunais, String nationalite, String numtel, String email, String codecat, String lingustique, String photo) { super(); this.idperaero = idperaero; this.nompe = nompe; this.prenom = prenom; this.residence = residence; this.datenais = datenais; this.lieunais = lieunais; this.nationalite = nationalite; this.numtel = numtel; this.email = email; this.codecat = codecat; this.lingustique = lingustique; this.photo = photo; } public PersonnelAeronautique(String nompe, String prenom, String residence, Date datenais, String lieunais, String nationalite, String numtel, String email, String codecat, String lingustique, String photo) { super(); this.nompe = nompe; this.prenom = prenom; this.residence = residence; this.datenais = datenais; this.lieunais = lieunais; this.nationalite = nationalite; this.numtel = numtel; this.email = email; this.codecat = codecat; this.lingustique = lingustique; this.photo = photo; } public PersonnelAeronautique(String nompe, String prenom, String residence) { super(); this.nompe = nompe; this.prenom = prenom; this.residence = residence; } public PersonnelAeronautique(String nompe) { super(); this.nompe = nompe; } public Long getIdperaero() { return idperaero; } public void setIdperaero(Long idperaero) { this.idperaero = idperaero; } public String getNompe() { return nompe; } public void setNompe(String nompe) { this.nompe = nompe; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getResidence() { return residence; } public void setResidence(String residence) { this.residence = residence; } public Date getDatenais() { return datenais; } public void setDatenais(Date datenais) { this.datenais = datenais; } public String getLieunais() { return lieunais; } public void setLieunais(String lieunais) { this.lieunais = lieunais; } public String getNationalite() { return nationalite; } public void setNationalite(String nationalite) { this.nationalite = nationalite; } public String getNumtel() { return numtel; } public void setNumtel(String numtel) { this.numtel = numtel; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getCodecat() { return codecat; } public void setCodecat(String codecat) { this.codecat = codecat; } public String getLingustique() { return lingustique; } public void setLingustique(String lingustique) { this.lingustique = lingustique; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } }
1. personnels
2. formPersonnelAeronautiqueCode:
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 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"/> <title>Personnel Aeronautique</title> <link rel="stylesheet" type="text/css" href="../static/css/bootstrap.css" th:href="@{/css/bootstrap.css}" /> <link rel="stylesheet" type="text/css" href="../static/css/mystyle.css" th:href="@{/css/mystyle.css}" /> </head> <body> <div class="container spacer"> <!-- lien pour créer un nouveau personnel Quand on clic, on fait appel à la méthode formPersonnelAeronautique --> <a th:href="@{formPersonnelAeronautique}">Noueau Personnel</a> <!-- ce formulaire fait appel à la méthode index du context de la même page--> <form th:action="@{index}" method="get"> <div class="form-group"> <label>Nom du personnel</label> <input type="text" name="motcle" th:value="${motCle}"/><!-- le mot cle provenant du model doit rester afficher --> <button type="submit">Chercher</button> </div> </form> </div><!-- fin formulaire recherche --> <div class="container spacer"> <table class="table table-striped"> <thead> <tr> <th>ID</th> <th>NOM</th> <th>PRENOM</th> <th>PHOTO</th> </tr> </thead> <tbody> <tr th:each="e:${pagePersonnels.content}"> <td th:text="${e.idperaero}"></td> <td th:text="${e.nompe}"></td> <td th:text="${e.prenom}"></td> <td th:text="${e.photo}"></td> </tr> </tbody> </table> </div><!-- fin div principal --> <div class="container"> <ul class="nav nav-pills"> <li th:each="p:${pages}" th:class="${p==pageCourante}?active:''"> <!-- on fait appel a methode index du controlleur initialiser par la page courante provenant du model et du motcle venant du model qui est affecté au champs motcle du formulaire --> <a th:text="${p}" th:href="@{index(page=${p},motcle=${motCle})}"> </a> </li> </ul> </div><!-- fin div pagination --> </body> </html>
Code:
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 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"/> <title>Nouveau Personnel Aeronautique</title> <link rel="stylesheet" type="text/css" href="../static/css/bootstrap.css" th:href="@{/css/bootstrap.css}" /> <link rel="stylesheet" type="text/css" href="../static/css/mystyle.css" th:href="@{/css/mystyle.css}" /> </head> <body> <div class="col-md-6 col -sm-6 col-xs-12 spacer col-md-offset-3" > <div class="panel panel-default"> <div class="panel-heading">Saisie d'un personnel aeronautique</div> <div class="panel-boy"> <!-- formulaire ici --> <form th:action="@{savePersonnel}" method="POST" th:object="${personnelAe}"><!-- chercher dans le model un objet nommé personnelAe --> <div class="form-group"> <label class="control-label">Nom</label> <input type="text" th:field="*{nompe}" class="form-control"/> <span th:erros="*{nompe}"></span> </div> <div class="form-group"> <label class="control-label">Prenom</label> <input type="text" th:field="*{prenom}" class="form-control"/> <span></span> </div> <div class="form-group"> <label class="control-label">Code Catégorie</label> <input type="text" th:field="*{codecat}" class="form-control"/> <span></span> </div> <div class="form-group"> <label class="control-label">Residence</label> <input type="text" th:field="*{residence}" class="form-control"/> <span th:erros="*{residence}"></span> </div> <div class="form-group"> <label class="control-label">Date de naissance</label> <input type="date" th:field="*{datenais}" class="form-control"/> <span th:erros="*{datenais}"></span> </div> <div class="form-group"> <label class="control-label">Lieu de naissance</label> <input type="text" th:field="*{lieunais}" class="form-control"/> <span th:erros="*{lieunais}"></span> </div> <div class="form-group"> <label class="control-label">Nationalité</label> <input type="text" th:field="*{nationalite}" class="form-control"/> <span></span> </div> <div class="form-group"> <label class="control-label">Téléphone</label> <input type="text" th:field="*{numtel}" class="form-control"/> <span th:erros="*{numtel}"></span> </div> <div class="form-group"> <label class="control-label">Email</label> <input type="text" th:field="*{email}" class="form-control"/> <span></span> </div> <div class="form-group"> <label class="control-label">Niveau langue</label> <input type="text" th:field="*{lingustique}" class="form-control"/> <span></span> </div> <div class="form-group"> <label class="control-label">Photo</label> <input type="file" th:field="*{photo}" class="form-control"/> <span></span> </div> <div> <button type="submit" class="btn btn-primary">Sauvegarder</button> </div> </form> </div> </div><!-- fin panel formulaire --> </div><!-- div principal --> </body> </html>
L'exécution
Pièce jointe 366359
si on clique sur le lien Nouveau Personnel
Pièce jointe 366360
Pièce jointe 366364
Normaement si on ne remplit pas quelques champs, après avoir cliquer sur sauvegarder, la validation du formulaire pour le champs obligatoire devrait se faire.