Spring boot, thymeleaf @PostMapping 405错误消息



我是新来的,也是新来的java编程。我一直在挣扎第二天现在与@PostMapping在春季启动MVC。我不明白我的代码出了什么问题。我正在做一个非常简单的银行账户项目。

我不知道在执行存款方法中连接AccountController中的@PostMapping有什么问题,并使用thymeleaf形式,这应该增加存款到账户余额。我刚开始学习春天,所以请温柔一点:)

`@Controller
public class AccountController {
@Autowired
private AccountService accountService;
@Autowired
private AccountRepository accountRepository;
// display all accounts
@AliasFor(value = "accounts")
@GetMapping("/accounts")
public String showAccountsList(Model model) {
List<Account> listAccounts = accountService.listAll();
model.addAttribute("listAccounts", listAccounts);
return "accounts";
}
@Autowired
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
//get Account by Id
@GetMapping("accounts/{id}")
public ResponseEntity<Account> getAccountById(@PathVariable Long id) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
return ResponseEntity.ok(account);
}
@GetMapping("/account_operations/{id}")
public String getAccountDetailsById(@PathVariable Long id, Model model) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
model.addAttribute("account", account);
return "account_operations";
}
@GetMapping("/account_operations/deposit/{id}")
public String makeDeposit(@PathVariable Long id, Model model) {
Account account = accountRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Account with id: " + id + " does not exist"));
model.addAttribute("account", account);
return "deposit";
}
@PostMapping(value = "/account_operations/deposit/{id}")
public String executeDeposit(@ModelAttribute("amount") double amount, Account account, RedirectAttributes ra) {
accountService.executeDeposit(account, amount);
ra.addFlashAttribute("message", "The customer has been saved successfully.");
return "redirect:/customer_accounts";
}`
`<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Deposit</title>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css"/>
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid text-center">
<div><h2>Deposit</h2></div>
<div class="m-3">
<a class="h4" th:href="@{/accounts}">Back to accounts</a>
<p>test</p>
</div>
<div>
<div th:if="${account}">
<h2>Deposit to account:</h2>
<h3>[[${account.accountNumber}]]</h3>
<p>[[${account.balance}]]</p>
<p>[[${account.accountType}]]</p>
</div>
<div>
<h2>Amount to deposit</h2>
<form th:action="@{/account_operations/deposit}" method="post" th:object="${account}" style="max-width: 500px; margin: 0 auto; font-size: small">
<div class="mb-3">
<label for="amount" class="form-label">Amount</label>
<input type="number" th:value="${amount}" th:field="*{amount}" name="amount" id="amount" class="form-control form-control-sm" placeholder="Deposit Amount" required>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary m-2">Deposit</button>
<button type="button" class="btn btn-secondary m-2" onclick="cancelForm()">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>`

`@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
@EntityListeners(AuditingEntityListener.class)
@SecondaryTable(name = "customer", pkJoinColumns = @PrimaryKeyJoinColumn(name = ""))
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private double balance;
private double interest;
private String accountNumber;
private AccountType accountType;
private double amount;

@ManyToOne
@JoinColumn(name = "customer_id", insertable = false, updatable = false)
@JsonBackReference
private Customer customer;
public Account(double balance, double interest, AccountType accountType) {
this.balance = balance;
this.interest = interest;
this.accountNumber = generateIbanAccountNumber();
this.accountType = accountType;
}
public String generateIbanAccountNumber() {
return new Iban.Builder().countryCode(CountryCode.PL).bankCode("696").buildRandom().toString();
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}`

表单中应该有csrf标记。把下面的代码放在表单结束标签前。

<input 
type="hidden" 
th:name="${_csrf.parameterName}" 
th:value="${_csrf.token}" />

相关内容

最新更新