3

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

2
  • stackoverflow.com/questions/5133564/… might be useful - you need to POST the form. Commented Aug 14, 2012 at 8:06
  • 1
    I have added an answer. Your approach should have worked, I am guessing that you are missing the HiddenHttpMethodFilter, I have added that information in my answer Commented Aug 14, 2012 at 12:48

2 Answers 2

4

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) 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks for you answer. Wouldn't changing the @RequestMapping annotation to GET involve any security issues?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.