Delete or put methods in thymeleaf
Asked Answered
I

6

20

I would like to call @RequestMapping(value = "", method = RequestMethod.DELETE) in spring from thymeleaf form.

Is there any possibility to call delete or put request mapping methods from thymeleaf?

please give your suggestion on this.

Illume answered 17/6, 2014 at 5:4 Comment(0)
A
33

You could use th:method for this:

<form th:object="${commandObject}" th:action="@{/urlToCall}" th:method="delete">

This will generate a hidden input element, which will be picked up by Spring MVC:

<input type="hidden" name="_method" value="delete">
Angara answered 15/7, 2014 at 8:37 Comment(1)
For me I had to add spring.mvc.hiddenmethod.filter.enabled=trueto my application.properties file to work.Xantho
G
18

When we use th:method="PUT", thymeleaf creates hidden input as in the screenshot below. Then HiddenHttpMethodFilter converts posted method parameters into HTTP methods.

httpmethodfilter-conversion

HiddenHttpMethodFilter disabled by default in Spring Boot 2.2.

You can enable this by adding spring.mvc.hiddenmethod.filter.enabled=true to your application.properties file.

Glossary answered 8/9, 2020 at 14:46 Comment(3)
the best answerTier
I'm glad I helped :)Glossary
Really the best answerOneil
C
10

Thymeleaf is an HTML template engine. HTML does not support put or delete HTTP methods for its method attribute. So, no, you can't. However, you have alternatives.

You can use javascript to send the request asynchronously. In that case, you can send any type of HTTP request.

You can also use a HiddenHttpMethodFilter with a hidden _method=put <input> element as described here. Something like

<input name="_method" type="hidden" value="PUT" />
Cumae answered 17/6, 2014 at 5:7 Comment(0)
H
2

Only solution I've found - use js:

Add scrypt:

<script th:inline="javascript">
    function sendDelete(url) {
        var xhttp = new XMLHttpRequest();
        xhttp.open("DELETE", url, true);
        xhttp.onload = function () {
            let responseURL = xhttp.responseURL;
            console.log("Redirecting to:", responseURL);
            window.location.replace(responseURL);
        };
        xhttp.send();
    }
</script>

and add calling link (or change to button):

<a type="button" th:with="url = @{<your_url>}" th:onclick="sendDelete([[${url}]])">Delete</a>
Hirza answered 17/3, 2020 at 6:59 Comment(0)
A
1

In my case, I use it and it works well.:

try this instead:

spring.mvc.hiddenmethod.filter.enabled=true

Apiece answered 3/7, 2023 at 20:5 Comment(0)
R
0

I am currently doing research on this subject. As far as I understand, we can perform these operations without the need to use PUT or DELETE methods. So you may not need to use JavaScript or anything else. These operations can be done using only Thymeleaf, HTML and Spring.

If someone has come here for this and is looking for such a solution, please check the post at this address. It was useful for me.

Here's link: Spring Boot CRUD Thymeleaf

Reef answered 22/1, 2021 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.