问题: 我尝试检索我拥有的所有数据,然后编辑表中的列日期和/或用餐时间。 如代码所示,我有两种方法,一种用于 GET,另一种用于提交我想要编辑数据的表单。 我的问题是我无法处理获取列表。它向我显示一个错误或一个空列表。
控制器:
@GetMapping("/table-of-contents")
public String showTableOfContents(Model model) {
transactions = getTableOfContents();
model.addAttribute("transactionList", transactions);
return "table_of_contents";
}
@PostMapping("/submitTransactions")
public String updateTableOfContents(@ModelAttribute("transactionList") List<TransactionMealList> transactions) {
// process of transactions
return "redirect:/table-of-contents";
}
和前端代码:
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th class="text-center" th:text="#{table.sequence}"> Number</th>
<th class="text-center" th:text="#{full.name.english}">Full Name</th>
<th class="text-center" th:text="#{date.transaction}">Date</th>
<th class="text-center" th:text="#{breakfast}">Breakfast</th>
<th class="text-center" th:text="#{lunch}">Lunch</th>
<th class="text-center" th:text="#{lunch}">Dinner</th>
<th class="text-center" th:text="#{restaurants}">Restaurant</th>
</tr>
</thead>
<tbody>
<tr style="border: #757171 solid 1pt" th:each="transaction, iterStat : *{transactionList}">
<td th:text="${transaction.employeeHrId} ">123
</td>
<td th:text="${transaction.employeeFullName}">Some name
</td>
<td>
<input class="form-control" type="date"
th:field="*{transactionList[__${iterStat.index}__].mealDate}" id="validFrom"
name="validFrom"
style="color: #FFFFFF">
</td>
<td>
<input type="checkbox" th:field="*{transactionList[__${iterStat.index}__].isBreakfast}"
style="width:20px; height:20px;"/>
</td>
<td>
<input type="checkbox" th:field="*{transactionList[__${iterStat.index}__].isLunch}"
style="width:20px; height:20px;"/>
</td>
<td>
<input type="checkbox" th:field="*{transactionList[__${iterStat.index}__].isDinner}"
style="width:20px; height:20px;"/>
</td>
<td th:text="${transaction.restaurantName}">A
</td>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-outline-light" type="button" id="submitButton">Submit</button>
</form>
</div>
尝试使用@RequestParam
@PostMapping("/submitTransactions")
public String updateTableOfContents(@RequestParam List<TransactionMealList> transactionList) {
// Process the updated transactions
return "redirect:/table-of-contents";
}