简体   繁体   English

删除控制器不起作用

[英]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 . 删除后,必须重定向到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? 问题是,当我点击Delete它只是刷新页面并且对象在数据库上持久存在,这里有什么问题?

  • I try send a DELETE request like curl -X "DELETE" http://localhost:8080/my-app/users/18 but this didn't work. 我尝试发送一个DELETE请求,如curl -X "DELETE" http://localhost:8080/my-app/users/18但这不起作用。

There are a bunch of methods available when communicating over HTTP. 通过HTTP进行通信时,有许多方法可用 The most common ones are GET, PUT, POST and DELETE. 最常见的是GET,PUT,POST和DELETE。

In your controller you declare that you expect a DELETE-request: 在您的控制器中,您声明您期望DELETE请求:

@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. 默认情况下,浏览器不支持此功能 - 浏览器仅支持POST和GET。 In order to send a DELETE-request from the browser you must use JavaScript. 要从浏览器发送DELETE请求,您必须使用JavaScript。

One alternative is to use eg jQuery's ajax-method 一种替代方法是使用例如jQuery的ajax方法

$.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: 测试DELETE请求的一种方法是使用命令cUrl:

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

As others have mentioned, you are not actually sending a HTTP DELETE request. 正如其他人所提到的,您实际上并没有发送HTTP DELETE请求。 Your delete button is part of a form post so when you submit the form it actually sends a HTTP POST request. 您的删除按钮是表单帖子的一部分,因此当您提交表单时,它实际上会发送HTTP POST请求。 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. 其他人已经演示了一些调用DELETE(Ajax和CURL)的方法,但我发现最简单的方法是在你喜欢的浏览器上安装一个插件。 If you're using Chrome you could try something like the Advanced Rest Client extension etc. 如果您使用的是Chrome,则可以尝试使用Advanced Rest Client扩展等功能。

as others have stated your html form will send POST as it is your action. 正如其他人所说,你的html表格将发送POST,因为这是你的行动。

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 如果你想保留按钮并在没有javascript(AJAX调用)的情况下进行删除,那么你应该尝试更改java端的url模式,并将删除按钮html分成单独的形式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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