Bonjour à toutes et à tous,
toujours suivant le tuto de Serge Tahé, j'ai des erreurs liées à la verification de champs id, nom et age comme indiqué ici:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('id')" (template: "vue-15" - line 20, col 16) ...
J'avoue que j'ai du mal à comprendre cette erreur d'autant plus que je veux afficher le formulaire avant toute action. Une partie des codes concernés:
- L'entité:
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
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
public class SecuredPerson {
 
	@Range(min = 1)
	private int id;
 
	@Length(min = 4, max = 10)
	private String nom;
 
	@Range( min = 8, max = 14)
	private int age;
 
	// Constructeurs
	public SecuredPerson() {
 
	}
 
	/*
	public SecuredPerson(String nom, int age) {
		this.nom = nom;
		this.age = age;
	}
	//*/
 
	public SecuredPerson(int id, String nom, int age) {
		this.id = id;
		this.nom = nom;
		this.age = age;
	}
 
	@Override
	public String toString() {
		return String.format("[id=%d, nom=%s, age=%d]", id, nom, age);
	}
 
	////////////////////////
	// getters et setters //
	////////////////////////
 
	//id
	public Integer getId() { 
		return id;
	}
 
	public void setId(Integer id) {
		this.id=id;
	}
 
	//nom
	public String getNom() { 
		return nom;
	}
 
	public void setNom(String nom) {
		this.nom=nom;
	}
 
	//age
	public int getAge() { 
		return age;
	}
 
	public void setAge(int age) {
		this.age=age;
	}
 
 
}
- Le controleur:
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
 
....
// Affichage d'un formulaire
	@RequestMapping(value = "/v15", method = RequestMethod.GET)
	public String v15(SecuredPerson p) {
		return "vue-15";
	}
 
 
	  // -------- Validation d'un modèle ----------------
 
	  @RequestMapping(value = "/v16", method = RequestMethod.POST) 
	  public String v16(@Valid SecuredPerson p, BindingResult result) { 
		  // Erreur ? 
		  if (result.hasErrors()) {
			  return "vue-15"; 
		  }else { 
			  return "vue-16"; 
		  } 
	 }
 
}
- Le formulaire v15:
Code HTML : 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
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<title th:text="#{title}">Spring 4 MVC</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>
		<form action="/someURL" th:action="@{/v16}" method="post">
			<h2 th:text="#{personne.formulaire.titre}">Entrez les informations suivantes</h2>
			<div th:object="${securedPerson}">
				<table>
					<thead></thead>
						<tbody>
							<tr>
								<td th:text="#{personne.id}">Id :</td>
								<td>
									<input type="text" name="id" value="11" th:value="*{id}" />
								</td>
								<td>
									<span th:if="${#fields.hasErrors('id')}" th:errors="*{id}" style="color: red">Identifiant erroné</span><!--  -->
								</td>
							</tr>
							<tr>
								<td th:text="#{personne.nom}">Nom :</td>
								<td>
									<input type="text" name="nom" value="Tintin" th:value="*{nom}" />
								</td>
								<td>
									<span th:if="${#fields.hasErrors('nom')}" th:errors="*{nom}" style="color: red">Nom erroné</span><!--  -->
								</td>
							</tr>
							<tr>
								<td th:text="#{personne.age}">Age :</td>
									<td>
										<input type="text" name="age" value="17" th:value="*{age}" />
									</td>
								<td>
									<span th:if="${#fields.hasErrors('age')}" th:errors="*{age}" style="color: red">Âge erroné</span><!--   -->
								</td>
							</tr>
						</tbody>
					</table>
				<input type="submit" value="Valider" th:value="#{personne.formulaire.valider}" />
				<ul>
					<li th:each="err : ${#fields.errors('*')}" th:text="${err}" style="color: red" />
				</ul>
			</div>
		</form>
	</body>
</html>

- Le formulaire v16:
Code HTML : 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
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<title th:text="#{title}">Spring 4 MVC</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>	
	<body>
		<h2 th:text="#{personne.formulaire.saisies}">Voici vos saisies</h2>
		<div th:object="${securedPerson}">
			<p>
				<span th:text="#{personne.id}">Id :</span>
				<span th:text="*{id}">14</span>
			</p>
			<p>
				<span th:text="#{personne.nom}">Nom :</span>
				<span th:text="*{nom}">Bill</span>
			</p>
			<p>
				<span th:text="#{personne.age}">Age :</span>
				<span th:text="*{age}">56</span>
			</p>
		</div>
	</body>
</html>