gpt4 book ai didi

java - 表格列表中的 Spring Thymeleaf

转载 作者:行者123 更新时间:2023-12-01 09:48:45 25 4
gpt4 key购买 nike

我从 GET 请求页面传递多个对象。其中之一是 ReplacedPartList 作为 ReplacedPart 的列表。

ReplacedPart.java

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "replaced_part_id")
private Long replacedPartId;

@Column(name = "maintain_id")
private Long maintainId;

@Column(name = "part_serial_no")
private String partSerialNo;

@Column(name = "quantity")
private Long quantity;

@Column(name = "unit_price")
private Double unitPrice;

@Column(name = "total_price")
private Double totalPrice;

//GETTERS and SETTERS

我的 Controller 的 GET 方法的一部分

List<ReplacedPart> replacedPartList = new ArrayList<>();

for (int i = 0; i < 7; i++) {
ReplacedPart replacedPart = new ReplacedPart();
replacedPartList.add(replacedPart);
}
model.addAttribute("replacedPartList", replacedPartList);

我退回的表格的一部分

<tr th:each="replacedPart, stat : ${replacedPartList}">
<td th:text="${__${stat.index}__}"></td>
<td><input type="text" th:value="${replacedPartList[__${stat.index}__].partSerialNo}" th:field="${replacedPartList[__${stat.index}__].partSerialNo}"></td>
<td><input type="text" th:value="${replacedPartList[__${stat.index}__].quantity}" th:field="${replacedPartList[__${stat.index}__].quantity}"></td>
<td><input type="text" th:value="${replacedPartList[__${stat.index}__].unitPrice}" th:field="${replacedPartList[__${stat.index}__].unitPrice}"></td>
<td><input type="text" th:value="${replacedPartList[__${stat.index}__].totalPrice}" th:field="${replacedPartList[__${stat.index}__].totalPrice}"></td>
</tr>

错误消息

BindingResult 和 bean 名称“replacedPartList[0]”的普通目标对象都不能作为请求属性

而且 iy 只是 GET 请求,甚至不是 POST。我该如何解决这个问题?

最佳答案

您没有使用 doc 中指定的正确语法。

试试这个:

    <tr th:each="replacedPart, rpStat : *{replacedPartList}">
<td th:text="${rpStat.index}"></td>
<td><input type="text" th:value="*{replacedPartList[__${rpStat.index}__].partSerialNo}" th:field="*{replacedPartList[__${rpStat.index}__].partSerialNo}"></td>
<td><input type="text" th:value="*{replacedPartList[__${rpStat.index}__].quantity}" th:field="*{replacedPartList[__${rpStat.index}__].quantity}"></td>
<td><input type="text" th:value="*{replacedPartList[__${rpStat.index}__].unitPrice}" th:field="*{replacedPartList[__${rpStat.index}__].unitPrice}"></td>
<td><input type="text" th:value="*{replacedPartList[__*{rpStat.index}__].totalPrice}" th:field="*{replacedPartList[__${rpStat.index}__].totalPrice}"></td>
</tr>

当使用列表时,是否要显示它,因此您不必在表单中使用它。表单通过“th:object”属性绑定(bind)到对象。因此,如果您要填充它,它必须是您的模型维护类的一部分。

这是完整的 example关于如何操作列表。

关于java - 表格列表中的 Spring Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com