【发布时间】:2020-06-02 10:08:04
【问题描述】:
我正在尝试将表单输入提取为我的 Spring Boot + Thymeleaf 应用程序中的页面片段。
我正在做这样的事情:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>form controls</title>
</head>
<body>
<div class="form-group" th:fragment="text-input (fieldName, fieldLabel)">
<label th:text="${fieldLabel}">label</label>
<input th:field="*{${fieldName}}" type="text" class="form-control"/>
<div th:if="${#fields.hasErrors('${fieldName}')}">
<span th:errors="*{${fieldName}}" class="text-danger"></span>
</div>
</div>
</body>
</html>
我就是这样使用片段的:
<div th:replace="form-controls :: text-input (fieldName='firstName', fieldLabel='Name')">
但是我收到了这个错误:
org.springframework.beans.NotReadablePropertyException: Invalid property '${fieldName}' of bean class [my.app.Dto]: Bean property '${fieldName}' is not readable or has an invalid getter method
所以参数fieldName没有正确解析...
我的代码有问题吗?是否可以在 Thymeleaf 中实现类似于我的片段的东西?
【问题讨论】:
标签: spring-boot thymeleaf