davhab
davhab

Reputation: 805

SpringMVC: Problems with specifying method in form to be DELETE

I am trying to delete a resource on my server and would like to do this via an ordinary link on my webpage.

I understand that when clicking on a link we cannot send a DELETE request to the server so I tried working around this with

<form id="aux_form" action="environment/">
   <input type="hidden" name="_method" value="delete">
   <input type="hidden" name="id" value="${env.id}">
</form>

and my Spring controller methods is annotated with

@RequestMapping(value = "/environment/", method = RequestMethod.DELETE)

However, I receive the error message "The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported)." so I know that my controller method is not called and the delete request is not properly mapped.

Would appreciate if anyone could tell me how to properly send this delete request.

Thanks :)

Upvotes: 3

Views: 1419

Answers (2)

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

This should work:

Register this filter in your web.xml file, this would transform the _method hidden parameter in the form to a DELETE Http request:

<filter>
    <filter-name>HttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>HttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Now your request can be handled by a handler of this type:

@RequestMapping(value = "/environment/", method = RequestMethod.DELETE)

Upvotes: 4

danny.lesnik
danny.lesnik

Reputation: 18639

You can't send DELETE request from a <form> tag. in your code you are still sending it as GET.

You should apply ajax based solution.

$.ajax({
    url: '/environment/',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});

or map your @RequestMapping annotation to GET.

Upvotes: 0

Related Questions