在特定 View 上,我确实有一个可扩展的员工列表。每个员工都有 2 个字段,分别是 hoursWorked
和 advancePayments
。单击 1 个按钮,我想发布整个表格。为了实现此目的,我将一个列表发送到包含多个 POJO(基于员工数量)的 View 。
添加到列表中的 POJO 如下所示:
@Setter
@Getter
@NoArgsConstructor
public class WorkdayCommand {
private Long employeeId;
private Integer hoursWorked;
private Integer advancePayment;
}
在 Controller 中,我确实有一行将列表添加到模型中:
model.addAttribute("workdayCommands", employeeService.getListOfWorkdayCommandsWithIds());
以及形成实际列表的方法:
public List<WorkdayCommand> getListOfWorkdayCommandsWithIds(){
List<WorkdayCommand> listOfCommands = new ArrayList<>();
List<Long> listOfIds = employeeRepository.getListOfIds();
for(int i = 0; i < employeeRepository.getNumberOfEmployees(); i++){
WorkdayCommand workdayCommand = new WorkdayCommand();
workdayCommand.setEmployeeId(listOfIds.get(i));
listOfCommands.add(workdayCommand);
}
return listOfCommands;
}
现在,我的观点有问题:
<div class="table-responsive" th:if="${not #lists.isEmpty(employees)}">
<form th:object="${workdayCommands}" th:action="@{/addworkday}">
some table headers...
<tr th:each="employee : ${employees}">
<td><a href="#" role="button" th:href="@{'/employee/' + ${employee.id}}" th:text="${employee.name}">Mike Kowalsky</a></td>
<td><input type="text" class="form-control" placeholder="Enter hours" th:field="*{hoursWorked}"></td>
<td><input type="text" class="form-control" placeholder="Enter payment" th:field="*{advancePayment}"></td>
<td><input type="hidden" th:field="*{id}"/></td>
</tr>
</form>
</div>
到目前为止,我不断收到错误:
NotReadablePropertyException: Invalid property 'hoursWorked' of bean class [java.util.ArrayList]: Bean property 'hoursWorked' is not readable or has an invalid getter method
如何正确地将数组列表与 View 绑定(bind)?我猜问题是数组列表中没有 hoursWorked
这样的字段。我应该使用什么 th:field
参数来获取实际的 WorkdayCommand.hoursWorked
字段,然后迭代列表以获取所有员工?如果您需要更多信息,请随时询问。
我正在尝试这样的事情:
th:field="*{[__${iter.index}__].hoursWorked}"
...但这仍然不起作用。我无法理解列表中的第一个 POJO。
编辑2
在单个表行中,我确实有一些员工信息以及 2 个输入和 2 个按钮。每行的创建归功于:
<tr th:each="employee : ${employees}">
当点击提交按钮时,会创建一个新的 Workday 对象,然后将其保存到数据库中。发生这种情况时,需要将工作日与相应的员工关联起来。所以我的:
<tr th:each="employee : ${employees}">
...我还分配了一个隐藏的 id
字段。然后我们有 WorkdayCommand
从 View 中收集所有信息。因此,employeeId
字段是我将工作日与相应员工关联的方式。它应该使用为每个显示所有信息传递的 id
值。希望现在已经清楚了。
最佳答案
您不能直接绑定(bind)到 ArrayList
作为 th:object
。相反,您应该创建一个将 ArrayList 作为属性的对象。
@Setter
@Getter
@NoArgsConstructor
public class WorkdayForm {
private List<WorkdayCommand> workdayCommands = new ArrayList<>();
}
然后循环,并像这样绑定(bind)它们:
<form th:object="${workdayForm}" th:action="@{/addworkday}">
<!-- table headers -->
<tr th:each="workdayCommand, i : ${workdayForm.workdayCommand}">
<td><input type="text" class="form-control" placeholder="Enter hours" th:field="*{workdayCommands[__${i.index}__].hoursWorked}"></td>
<td><input type="text" class="form-control" placeholder="Enter payment" th:field="*{workdayCommands[__${i.index}__].advancePayment}"></td>
<td><input type="hidden" th:field="*{workdayCommands[__${i.index}__].employeeId}"/></td>
</tr>
<!-- table footers -->
</form>
关于java - Spring MVC - Thymeleaf 表单与数组列表绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49258295/