Spring Thymeleaf和#fields.hasErrors
我正在为学校做这项作业。使用SpringMVC、HibernateJPA和Thymeleaf。下面的代码涉及一个名为“stringGrade”的特定属性。我想使用Hibernate验证器验证该字段中的输入。我似乎无法让特里梅拉夫读懂这句话。在视图中循环的arrayList具有“可交付成果[0].stringGrade”的name属性,依此类推,具体取决于可交付成果的数量。我尝试过使用“可交付成果[${stat.index}].name”,这会导致Thymeleaf失败,并出现以下错误: HTTP状态500-请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateProcessingException:异常评估SpringEL表达式:“#fields.hasErrors('Deliveries[0].stringGrade')”(menuItems/inputGrades:33) 我只想让Thymeleaf能够使用#fields.HasErrors和#fields.error读取值。以下是相关代码: 等级计算器型号:Spring Thymeleaf和#fields.hasErrors,spring,thymeleaf,spring-el,Spring,Thymeleaf,Spring El,我正在为学校做这项作业。使用SpringMVC、HibernateJPA和Thymeleaf。下面的代码涉及一个名为“stringGrade”的特定属性。我想使用Hibernate验证器验证该字段中的输入。我似乎无法让特里梅拉夫读懂这句话。在视图中循环的arrayList具有“可交付成果[0].stringGrade”的name属性,依此类推,具体取决于可交付成果的数量。我尝试过使用“可交付成果[${stat.index}].name”,这会导致Thymeleaf失败,并出现以下错误: HTTP
public class GradeCalculator {
private ArrayList<Deliverable> deliverables;
视窗:
<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变量表达式正确处理“可交付成果[${stat.index}].stringGrade”。我应该这样做:
<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>
-
输入不正确
<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>