簡體   English   中英

Spring MVC-Thymeleaf表單與arraylist綁定

[英]Spring MVC - Thymeleaf form binding with an arraylist

從特定的角度來看,我確實有一個可擴展的員工列表。 每個員工都有2個字段,分別是hoursWorkedadvancePayments 單擊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

如何正確綁定arraylist與視圖? 我想問題是在arraylist中沒有這樣的字段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值。 希望現在清楚了。

您不能直接將th:object綁定到ArrayList 相反,您應該創建一個具有ArrayList作為屬性的對象。

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

然后循環瀏覽,並像這樣綁定它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM