Spring MVC - Thymeleaf 表单与数组列表绑定

Mac*_*iaz 3 java spring spring-mvc thymeleaf

在特定的视图中,我确实有一个可扩展的员工列表。每个员工都有 2 个字段,即hoursWorked& advancePayments。单击 1 个按钮,我想发布整个表格。为了实现此目的,我将一个列表发送到包含多个 POJO(基于员工数量)的视图。

添加到列表中的 POJO 如下所示:

@Setter
@Getter
@NoArgsConstructor
public class WorkdayCommand {

    private Long employeeId;
    private Integer hoursWorked;
    private Integer advancePayment;
}
Run Code Online (Sandbox Code Playgroud)

在控制器中,我确实有一行将列表添加到模型中:

model.addAttribute("workdayCommands", employeeService.getListOfWorkdayCommandsWithIds());
Run Code Online (Sandbox Code Playgroud)

以及形成实际列表的方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

现在我的观点有问题:

<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>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我不断收到错误:

NotReadablePropertyException: Invalid property 'hoursWorked' of bean class [java.util.ArrayList]: Bean property 'hoursWorked' is not readable or has an invalid getter method
Run Code Online (Sandbox Code Playgroud)

如何正确地将数组列表与视图绑定?我想问题是 arraylist 中没有这样的字段hoursWorked。我应该使用什么th:field参数来获取实际WorkdayCommand.hoursWorked字段,然后迭代列表以获取所有员工?如果您需要更多信息,请随时询问。

我正在尝试类似的事情:

th:field="*{[__${iter.index}__].hoursWorked}"
Run Code Online (Sandbox Code Playgroud)

...但这仍然不起作用。我无法理解列表中的第一个 POJO。

编辑2

我的完整视图如下所示: 在此输入图像描述

在单个表行中,我确实有一些员工信息以及 2 个输入和 2 个按钮。每行的创建归功于:

 <tr th:each="employee : ${employees}">
Run Code Online (Sandbox Code Playgroud)

当点击提交按钮时,会创建一个新的 Workday 对象,然后将其保存到数据库中。发生这种情况时,需要将工作日与相应的员工关联起来。所以我的:

 <tr th:each="employee : ${employees}">
Run Code Online (Sandbox Code Playgroud)

...我还分配了一个隐藏id字段。然后我们就可以WorkdayCommand从视图中收集所有信息。因此,该employeeId字段是我将工作日与相应员工关联起来的方式。它应该使用id为每个显示所有信息传递的值。希望现在已经清楚了。

Met*_*ids 5

您不能直接绑定到ArrayListaS th:object。相反,您应该创建一个将 ArrayList 作为属性的对象。

@Setter
@Getter
@NoArgsConstructor
public class WorkdayForm {
    private List<WorkdayCommand> workdayCommands = new ArrayList<>();
}
Run Code Online (Sandbox Code Playgroud)

然后循环,并像这样绑定它们:

<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>
Run Code Online (Sandbox Code Playgroud)