@DeleteMapping Spring Boot

huangapple go评论73阅读模式
英文:

@DeleteMapping spring boot

问题

DELETE查询为什么对我不起作用。

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

这个映射有一个表单,但是返回错误消息“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>

如果我使用 @GetMapping,则一切正常,但我理解这是不正确的,因为在这种情况下不会执行删除请求。

如果我这样做,那么删除工作了,但PATCH不起作用。

<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>
</form>

如何使userId在外部表单中可用,即在循环之外?

我已重新设计任务,这些表单可以单独工作。如何使这个选择器关联两个表单。

<select th:name="userId" id="userid" th:form="form_update | form_delete">
    <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>
<input type="button" value="Add" onclick="window.location.href = 'add'"/>

<form action="#" th:id="form_delete" th:action="@{/delete}" th:object="${users}" th:method="delete">
    <input type="submit" value="Delete"/>
</form>

<form action="#" th:id="form_update" th:action="@{/update}" th:object="${users}" th:method="patch">
    <input type="submit" value="Update"/>
</form>
英文:

why is the DELETE query not working for me.

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

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

&lt;form th:object=&quot;${users}&quot;&gt;
    &lt;input type=&quot;button&quot; value=&quot;Add&quot; onclick=&quot;window.location.href = &#39;add&#39;&quot;/&gt;
        &lt;input th:formaction=&quot;@{/update}&quot; type=&quot;submit&quot; value=&quot;Update&quot; th:method=&quot;patch&quot; class=&quot;button&quot;/&gt;
        &lt;input th:formaction=&quot;@{/delete}&quot; type=&quot;submit&quot; value=&quot;Delete&quot; th:method=&quot;delete&quot;/&gt;
    &lt;select th:name=&quot;userId&quot;&gt;
        &lt;option th:each=&quot;user : ${users}&quot; th:value=&quot;${user.id}&quot;&gt;
            &lt;tr&gt;
                &lt;td th:text=&quot;${user.name}&quot;&gt;&lt;/td&gt;
                &lt;td th:text=&quot;${user.surname}&quot;&gt;&lt;/td&gt;
                &lt;td th:text=&quot;${user.age}&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/option&gt;
    &lt;/select&gt;
&lt;/form&gt;

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.

&lt;form th:object=&quot;${users}&quot; th:method=&quot;delete&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Add&quot; onclick=&quot;window.location.href = &#39;add&#39;&quot;/&gt;
    &lt;input th:formaction=&quot;@{/update}&quot; type=&quot;submit&quot; value=&quot;Update&quot; th:method=&quot;patch&quot; class=&quot;button&quot;/&gt;
    &lt;input th:formaction=&quot;@{/delete}&quot; type=&quot;submit&quot; value=&quot;Delete&quot;/&gt;
&lt;select th:name=&quot;userId&quot;&gt;
    &lt;option th:each=&quot;user : ${users}&quot; th:value=&quot;${user.id}&quot;&gt;
        &lt;tr&gt;
            &lt;td th:text=&quot;${user.name}&quot;&gt;&lt;/td&gt;
            &lt;td th:text=&quot;${user.surname}&quot;&gt;&lt;/td&gt;
            &lt;td th:text=&quot;${user.age}&quot;&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/option&gt;
&lt;/select&gt;

</form>

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

I have redone the task, these forms work separately. How do I make this select look at two forms.

&lt;select th:name=&quot;userId&quot; id=&quot;userid&quot; th:form=&quot;form_update | form_delete&quot; &gt;
&lt;option th:each=&quot;user : ${users}&quot; th:value=&quot;${user.id}&quot;&gt;
    &lt;tr&gt;
        &lt;td th:text=&quot;${user.name}&quot;&gt;&lt;/td&gt;
        &lt;td th:text=&quot;${user.surname}&quot;&gt;&lt;/td&gt;
        &lt;td th:text=&quot;${user.age}&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/option&gt;

</select>

&lt;input type=&quot;button&quot; value=&quot;Add&quot; onclick=&quot;window.location.href = &#39;add&#39;&quot;/&gt;

&lt;form action=&quot;#&quot; th:id=&quot;form_delete&quot;  th:action=&quot;@{/delete}&quot; th:object=&quot;${users}&quot; th:method=&quot;delete&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Delete&quot;/&gt;

</form>

&lt;form action=&quot;#&quot; th:id=&quot;form_update&quot;  th:action=&quot;@{/update}&quot; th:object=&quot;${users}&quot; th:method=&quot;patch&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Update&quot;/&gt;

</form>

答案1

得分: 2

HTML表单仅支持GETPOST方法。

如果您想使用DELETE方法,您将需要使用JavaScript。

英文:

HTML forms support only GET and POST methods.

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

huangapple
  • 本文由 发表于 2023年5月29日 02:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定