0

I have this controller:

  @DeleteMapping("/delete/{id}")
  public String deleteWallet(@PathVariable("id") long id, Model model) {
    Wallet wallet = walletRepository.findById(id)
            .orElseThrow(() -> new IllegalArgumentException("Invalid wallet Id:" + id));
    walletRepository.delete(wallet);
    model.addAttribute("wallets", walletRepository.findAll());
    return "redirect:/";
}

And this is HTML:

<table class="table table-striped table-hover" id="productTable">
<thead>
<tr class="success">
    <th>Wallet Name</th>
    <th>Initial Balance</th>
</tr>
</thead>
<tbody>
<tr th:each="wallet : ${wallet}">
    <td th:text="${wallet.walletName}"></td>
    <td th:text="${wallet.initialBalance}"></td>
    <td>
        <a th:href="@{/api/wallet/delete/{id}(id = ${wallet.id})}">Delete</a>
    </td>
</tr>
</tbody>

But when I press delete button its says:

Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

I'm confused, my method is @DeleteMapping

Anyway, I found a way:

HTML:

<form th:action="@{/api/wallet/delete/{id}(id=${wallet.id})}" 
th:object="${wallet}" method="post">
            <input type="hidden" th:field="${wallet}">Delete</input>
            <button type="submit" onClick="return confirm('sure?')"/>
        </form>

Controller:

 @PostMapping("/delete/{id}")
 public String deleteWallet(@PathVariable("id") long id, Model model) {
    walletService.deleteWalletById(id);
    return "redirect:/";
}

0 Answers0