Thymeleaf和#fields.hasErrors [英] Thymeleaf and #fields.hasErrors
问题描述
我正在为学校工作.使用SpringMVC,Hibernate JPA和Thymeleaf.下面的代码包含一个称为"stringGrade"的特定属性.我想使用Hibernate Validator验证该字段中的输入.我似乎无法让Thymeleaf读取该表达.在视图中循环的arrayList的名称属性为"deliveryables [0] .stringGrade",依存在的数量而定.我尝试使用"deliveryables [ $ {stat.index} ].name",这导致Thymeleaf失败,并显示以下错误:
I have this assignment I am working on for school. Using SpringMVC, Hibernate JPA, and Thymeleaf. The following code below involves a specific attribute called " stringGrade ". I want to validate the input in that field using Hibernate Validator. I cannot seem to get Thymeleaf to read the expression. The arrayList that is looped in the view has a name attribute of " deliverables[0].stringGrade " and so on depending on how many there are. I have tried using " deliverables[${stat.index}].name " and this causes Thymeleaf to fail with this error:
HTTP状态500-请求处理失败;嵌套的异常是org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:#fields.hasErrors('deliverables [0] .stringGrade')"(menuItems/inputGrades:33)
HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('deliverables[0].stringGrade')" (menuItems/inputGrades:33)
我要做的就是让Thymeleaf能够使用#fields.HasErrors和#fields.error读取值.以下是相关的代码:
All I want to do is have Thymeleaf be able to read the value using #fields.HasErrors and #fields.error. Below is the code that is relevant:
GradeCalculator模型:
GradeCalculator Model:
public class GradeCalculator {
private ArrayList<Deliverable> deliverables;
交付模型:
@Entity
@Table(name="Deliverables")
public class Deliverable implements Serializable {
@NotEmpty(message = "Required")
@Size(min = 1, max = 100, message = "Must be between 1 and 100")
@Digits(integer = 3, fraction = 0, message = "Must be a numeric value")
private String stringGrade; // String version of the grade ( Used for view input fields )
百里香叶视图:
<form th:object="${gradeCalculator}" action="#" th:action="@{/process/inputGrades}" method="POST" class="form-horizontal" role="form">
<div th:each="deliverable,stat : ${grades.deliverables}">
<div class="form-group">
<p>Deliverable Name<span th:text="${grades.deliverables[__${stat.index}__].name}" name="name" id="name" class="badge tab-space"></span></p>
<p>Deliverable Weight<span th:text="${grades.deliverables[__${stat.index}__].weight}" name="weight" id="weight" class="badge tab-space"></span></p>
<h3><span class="label">Grade:</span></h3>
<input type="text" th:field="${grades.deliverables[__${stat.index}__].stringGrade}" class="form-control" />
<ul class="help-inline" th:if="${#fields.hasErrors('deliverables[__${stat.index}__].stringGrade')}">
<li class="error" th:each="err : ${#fields.errors('deliverables[__${stat.index}__].stringGrade')}" th:text="${err}">Input is incorrect</li>
</ul>
</div>
</div>
<div class="form-group">
<div class="text-center col-sm-10 col-sm-offset-2 col-md-4 col-md-offset-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
推荐答案
找到了答案.我没有使用Thymeleaf变量表达式正确处理"deliveryables [ $ {stat.index} ].stringGrade".我应该一直在这样做:
Found the answer. I was not processing " deliverables[${stat.index}].stringGrade " correctly by using the Thymeleaf variable expression. I should have been doing this:
<ul class="help-inline" th:if="${#fields.hasErrors('${grades.deliverables[__${row.index}__].stringGrade}')}">
<li
class="error"
th:each="err : ${#fields.errors('${grades.deliverables[__${row.index}__].stringGrade}')}"
th:text="${err}">
Input is incorrect
</li>
</ul>
这篇关于Thymeleaf和#fields.hasErrors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!