Java 如何使用thymeleaf在具有动态列的数据表中显示输入值
我正在尝试创建一个带有数据表的表单列表,在其中放置集合中的一些输入值。事实上,我不知道如何显示与控制器连接的值,以正确显示和注册输入值 这是我要显示为表单列表的DTO:Java 如何使用thymeleaf在具有动态列的数据表中显示输入值,java,spring,spring-boot,spring-mvc,thymeleaf,Java,Spring,Spring Boot,Spring Mvc,Thymeleaf,我正在尝试创建一个带有数据表的表单列表,在其中放置集合中的一些输入值。事实上,我不知道如何显示与控制器连接的值,以正确显示和注册输入值 这是我要显示为表单列表的DTO: @Data public class ComponentPeriodValues { private int idComponent; private String description; private List<String> periods; private List<BigDecimal> v
@Data
public class ComponentPeriodValues {
private int idComponent;
private String description;
private List<String> periods;
private List<BigDecimal> values;
}
@数据
公共类组件值{
私有内部组件;
私有字符串描述;
私人名单期;
私有列表值;
}
所有记录的周期数相等,我正在尝试构建一个如下所示的数据表:
<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
<form action="#" th:action="@{/planning-components/save}"
th:object="${cpv}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>Description</th>
<th:block th:each="p : ${cpv[0].periods}">
<th th:text="${p}"></th>
</th:block>
</tr>
</thead>
<tbody>
<tr th:each="c : ${cpv}">
<td th:text="${c.description}" />
<th:block th:each="v : ${c.values}">
<td>
<input type="text" th:field="*{v}"
class="form-control mb-4 col-4"/>
</td>
</th:block>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
</div>
我的HTML如下所示:
<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
<form action="#" th:action="@{/planning-components/save}"
th:object="${cpv}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>Description</th>
<th:block th:each="p : ${cpv[0].periods}">
<th th:text="${p}"></th>
</th:block>
</tr>
</thead>
<tbody>
<tr th:each="c : ${cpv}">
<td th:text="${c.description}" />
<th:block th:each="v : ${c.values}">
<td>
<input type="text" th:field="*{v}"
class="form-control mb-4 col-4"/>
</td>
</th:block>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
</div>
描述
拯救
但是,当我使用此输入字段时,出现以下错误:
原因:org.springframework.beans.NotReadablePropertyException:bean类[java.util.ArrayList]的无效属性“v”:bean属性“v”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配
当我使用纯文本字段时,它会起作用
因此,我的问题是:我如何在这里使用输入字段来反映DTO中的更改,然后使用更新的DTO来处理数据库中的更新?我对百里香叶语法了解不多,但我了解spring mvc数据绑定的工作原理。您可能需要更改一些百里香叶语法,但这里是您需要做的- 将
ComponentPeriodValues
列表包装到另一个类中,如下所示
@数据
公共类ComponentPeriodCommand实现可序列化{
私有静态最终长serialVersionUID=1L;
私人名单CPV;
}
现在,要将值绑定到列表中,html的第一行、第二列等等都需要这样
根据这一点,你可以得到胸腺指数
描述
拯救
Hi@saifulislamplabon,我按照您的建议做了,但没有成功:(.得到此错误:由:org.springframework.beans.NotReadablePropertyException引起:bean类[java.util.ArrayList]的无效属性“值[0]”:bean属性值[0]'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?您需要创建一个包装列表的命令对象。请检查我的更新答案。它像一个符咒一样工作!这正是我要找的。谢谢你:)@guilhermescorreia如果它适合你,请将此答案标记为已接受,并始终进行追加投票。