简体   繁体   中英

Delete controller is not working

I'm really getting started with controllers for my small application, and i have this for 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, is working properly, but the second 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>

like you see, i have two buttons here, one for edit the object and one for delete it. Once deleted it, must redirect to https://<my domain>/users .

The problem is, when i click on Delete it just refresh the page and the object persist on the database, what is wrong here?

  • I try send a DELETE request like curl -X "DELETE" http://localhost:8080/my-app/users/18 but this didn't work.

There are a bunch of methods available when communicating over HTTP. The most common ones are GET, PUT, POST and DELETE.

In your controller you declare that you expect a DELETE-request:

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

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

One alternative is to use eg jQuery's ajax-method

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

One way of testing DELETE-requests is to use the command cUrl:

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

As others have mentioned, you are not actually sending a HTTP DELETE request. Your delete button is part of a form post so when you submit the form it actually sends a HTTP POST request. Others have demonstrated some ways to invoke a DELETE (Ajax, and CURL) but I find the easiest way is to install a plugin on your favourite browser. If you're using Chrome you could try something like the Advanced Rest Client extension etc.

as others have stated your html form will send POST as it is your action.

if you want to keep the button and do a delete without javascript (AJAX call) then you should try changing the url pattern in java side and also put the delete button html in a seperate form

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM