百里香和#fields.hasErrors
Thymeleaf and #fields.hasErrors
我正在为学校做这项作业。使用 SpringMVC、Hibernate JPA 和 Thymeleaf。以下代码涉及名为“stringGrade”的特定属性。我想使用 Hibernate Validator 验证该字段中的输入。我似乎无法让 Thymeleaf 读取表达式。在视图中循环的 arrayList 的名称属性为“deliverables[0].stringGrade”,依此类推,具体取决于有多少。我尝试使用“可交付成果[${stat.index}].name ”,这会导致 Thymeleaf 失败并出现此错误:
HTTP 状态 500 - 请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('deliverables[0].stringGrade')" (menuItems/inputGrades:33)
我只想让 Thymeleaf 能够使用#fields.HasErrors 和#fields.error 读取值。以下是相关代码:
成绩计算器型号:
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 变量表达式正确处理“可交付成果[${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>
我正在为学校做这项作业。使用 SpringMVC、Hibernate JPA 和 Thymeleaf。以下代码涉及名为“stringGrade”的特定属性。我想使用 Hibernate Validator 验证该字段中的输入。我似乎无法让 Thymeleaf 读取表达式。在视图中循环的 arrayList 的名称属性为“deliverables[0].stringGrade”,依此类推,具体取决于有多少。我尝试使用“可交付成果[${stat.index}].name ”,这会导致 Thymeleaf 失败并出现此错误:
HTTP 状态 500 - 请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('deliverables[0].stringGrade')" (menuItems/inputGrades:33)
我只想让 Thymeleaf 能够使用#fields.HasErrors 和#fields.error 读取值。以下是相关代码:
成绩计算器型号:
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 变量表达式正确处理“可交付成果[${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>