skip to Main Content

why is the DELETE query not working for me.

    @DeleteMapping("/delete")
    public String deleteUser(@RequestParam("userId") long id) {
        userService.deleteUser(id);
        return "redirect:/";
    }

There is a form for this mapping, but it returns me the Request method ‘GET’ is not supported.

<form th:object="${users}">
    <input type="button" value="Add" onclick="window.location.href = 'add'"/>
        <input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
        <input th:formaction="@{/delete}" type="submit" value="Delete" th:method="delete"/>
    <select th:name="userId">
        <option th:each="user : ${users}" th:value="${user.id}">
            <tr>
                <td th:text="${user.name}"></td>
                <td th:text="${user.surname}"></td>
                <td th:text="${user.age}"></td>
            </tr>
        </option>
    </select>
</form>

If I do @GetMapping, then everything works, but I understand that this is not correct, the delete request is not executed in this case.

If I do this, then delete works, but PATCH does not work.

<form th:object="${users}" th:method="delete">
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
    <input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
    <input th:formaction="@{/delete}" type="submit" value="Delete"/>
<select th:name="userId">
    <option th:each="user : ${users}" th:value="${user.id}">
        <tr>
            <td th:text="${user.name}"></td>
            <td th:text="${user.surname}"></td>
            <td th:text="${user.age}"></td>
        </tr>
    </option>
</select>

how to make userId available in an external form, that is, outside the loop

2

Answers


  1. To make the delete method work
    you should enable it by adding

     spring.mvc.hiddenmethod.filter.enabled=true 
    

    to your application.properties file.

    Login or Signup to reply.
  2. HTML forms support only GET and POST methods.

    If you want to use a DELETE method, you will have to use JavaScript.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search