在页面上,我有一个表单,除了输入金额、注解和日期的字段外,还有一个下拉菜单,允许用户从列表中选择一个项目。该下拉菜单是从数据库填充的,工作正常。现在,当用户从列表中选择一个项目并单击保存按钮将其选择插入数据库时,该字段在表中为空。其他字段保存良好,但该字段从下拉菜单没有保存,它的空。但是,我可以在MySQL和手动分配类别的字段,见下图。
用户点击“保存”后,情况如下:
但我可以从MySQL手动指定该字段,如您所见:
这是thymeleaf代码:
<form action="#" th:action="@{/api/transaction/saveTransaction/{walletId} (walletId=${id})}"
th:object="${transaction}" method="POST">
<input type="text" th:field="*{amount}" placeholder="Enter amount" class="form-control mb-4 col-4">
<input type="text" th:field="*{note}" placeholder="Enter note" class="form-control mb-4 col-4">
<input type="date" th:field="*{date}" class="form-control mb-4 col-4">
<select class="form-control" id="dropOperator">
<option value="0">Select category</option>
<option th:each="categories : ${categories}" th:value="${categories.name}"
th:text="${categories.name}"></option>
</select>
<button type="submit" class="btn btn-info col-2"> Save Wallet</button>
</form>
这是保存该表单的控制器:
@PostMapping("/saveTransaction/{walletId}")
public String saveTransaction(@PathVariable(value = "walletId") long walletId,
@ModelAttribute("transaction") Transaction transaction) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl user = (UserDetailsImpl) authentication.getPrincipal();
long userId = user.getId();
Wallet wallet = walletService.getWalletById(walletId);
transaction.setWallet(wallet);
transactionService.saveTransaction(transaction);
return "redirect:/api/wallet/userWallet/balance/" + userId;
}
这是显示表格的控制器:
@GetMapping("/showNewTransactionForm/{id}")
public String showNewTransactionForm(@PathVariable(value = "id") long id, Transaction transaction, Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl user = (UserDetailsImpl) authentication.getPrincipal();
long userId = user.getId();
model.addAttribute("userId", userId);
model.addAttribute("transaction", transaction);
List<Category> categories = categoryService.getAllCategories();
model.addAttribute("categories", categories);
return "new_transaction";
}
这是category
实体:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private Long id;
@Column(name = "category_name")
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private List<Transaction> transactions;
这是transaction
实体:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "transaction_id")
private Long id;
private double amount;
private String note;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "date")
private Date date;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category_name", referencedColumnName = "category_name")
private Category category;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="wallet_id", nullable=false)
private Wallet wallet;
有什么建议我可以尝试吗?我有一个错误的字段,说Cannot add or update a child row a foreign key constraint fails
,但我修复了它,现在没有任何错误,只是字段没有保存到数据库中,但是,正如我所说,我可以手动插入它成功地在MySQL中。
1条答案
按热度按时间dced5bon1#
也许你应该试着写一下,因为:字段等于
th:name
+th:value