SpringMVC: Problems with specifying method in form to be DELETE

html, spring, spring-mvc

Solution

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)

Problem

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 :)

Original source