Uninstall controller does not work

I really start with the controllers for my small application, and I have it now:

@RequestMapping("/users/{id}") public ModelAndView showMemeber(@PathVariable Integer id) { ModelAndView mav = new ModelAndView("user/show"); mav.addObject("title", "Show User"); mav.addObject("user", userService.findById(id)); return mav; } @RequestMapping(value="/users/{id}", method=RequestMethod.DELETE) public String deleteMemeber(@PathVariable Integer id) { userService.delete(id); return "redirect:users"; } 

the first one works fine, but the second one doesn't, I have the following view for the first controller:

 <div class="panel-heading">Personal information</div> <div class="panel-body"> <form method="post"> ... <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span> Edit</button> <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete {{ user.username }}?')"><span class="glyphicon glyphicon-remove"></span> Delete</button> </form> </div> 

As you can see, I have two buttons here: one for editing an object and one for deleting it. After removing it, you must redirect to https://<my domain>/users .

The problem is that when I click on Delete , it just refreshes the page and the object is saved in the database, what’s wrong here?

  • I am trying to send a Delete request as curl -X "DELETE" http://localhost:8080/my-app/users/18 , but this did not work.
+6
source share
3 answers

When communicating via HTTP, there are many methods. The most common are GET, PUT, POST and DELETE.

In the controller, you declare that you are expecting a DELETE request:

 @RequestMapping(value="/users/{id}", method=RequestMethod.DELETE) public String deleteMemeber(@PathVariable Integer id) {...} 

This is not supported by the default browser - the browser only supports POST and GET. To send a DELETE request from a browser, you must use JavaScript.

One option is to use, for example, jQuery ajax-method

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

One way to test DELETE queries is to use the cUrl command:

 curl -X DELETE "http://myhost:port/users/someUserId" 
+8
source

As already mentioned, you are not actually sending an HTTP DELETE request. Your delete button is part of the form message, so when you submit the form, it actually sends an HTTP POST request. Others have shown some ways to invoke DELETE (Ajax and CURL), but I find the easiest way is to install the plugin in your favorite browser. If you use Chrome, you can try something like the Advanced Rest Client extension, etc.

+1
source

as others have stated that your html form will submit a POST, as that is your action.

if you want to save the button and do the deletion without javascript (AJAX call) then you should try changing the url template in the java side, and also place the html delete button in a separate form

@RequestMapping (value = "/ users / delete / {id}", method = RequestMethod.POST)

0
source

Source: https://habr.com/ru/post/980225/


All Articles