【发布时间】:2018-03-13 14:06:36
【问题描述】:
在特定视图中,我确实有一个可扩展的员工列表。每个员工有 2 个字段,它们是 hoursWorked 和 advancePayments。只需单击 1 次按钮,我想发布整个表单。为了实现这一点,我向包含多个 POJO(基于员工数量)的视图发送了一个列表。
添加到列表中的 POJO 如下所示:
@Setter
@Getter
@NoArgsConstructor
public class WorkdayCommand {
private Long employeeId;
private Integer hoursWorked;
private Integer advancePayment;
}
在控制器中,我确实有一行可以将列表添加到模型中:
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
如何正确绑定数组列表和视图?我想问题是数组列表中没有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,它从视图中收集所有信息。所以employeeId 字段是我将工作日与相应员工相关联的方式。它应该使用为每个显示所有信息传递的id 值。希望现在清楚了。
【问题讨论】:
-
您还应该在
workdayCommands上进行迭代,就像在employees上一样 -
关于我在哪里做的任何线索?在
form标签中?
标签: java spring spring-mvc thymeleaf