春天Thymelaf在表格列表中

时间:2016-06-12 02:00:50

标签: java spring spring-mvc thymeleaf

我从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

我的控制器的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>

错误消息

Neither BindingResult nor plain target object for bean name 'replacedPartList[0]' available as request attribute

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

2 个答案:

答案 0 :(得分:1)

您没有使用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>

使用列表时是否要显示它,因此您不必在表单中使用它。表格与第一个对象绑定到一个对象&#39;属性。因此,如果您要填写它,它必须是您的模型维护课程的一部分。

以下是关于如何操作列表的完整example

答案 1 :(得分:0)

我用*课解决了我的问题。我创建了一个表单对象并将其发送到视图。然后我像这样使用@Valid private Maintain maintain; @Valid private Demand demand; private List<ReplacedPart> replacedPartList; public MaintainFormDto(Maintain maintain, Demand demand, List<ReplacedPart> replacedPartList) { this.maintain = maintain; this.demand = demand; this.replacedPartList = replacedPartList; } //GETTER and SETTERS 作为对象绑定。

<强> MaintainFormDto

MaintainFormDto formDto = new MaintainFormDto(maintain, demand, replacedPartList);
model.addAttribute("form", formDto);

<强> MaintainController

<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>

<强> form.html

{{1}}