我真的开始为我的小应用程序使用控制器,我现在有这个:
@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";
}
第一个工作正常,但第二个没有,我对第一个控制器有以下视图:
<div class="panel-heading">Personal information</div>
<div class="panel-body">
<form>
...
<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>
如您所见,我这里有两个按钮,一个用于编辑对象,一个用于删除它。一旦删除它,必须重定向到https://<my domain>/users
.
问题是,当我单击Delete
它时,只需刷新页面并且对象会保留在数据库中,这里有什么问题?
- 我尝试发送
DELETE
类似的请求,curl -X "DELETE" http://localhost:8080/my-app/users/18
但这没有用。 我尝试使用jQuery 的 ajax 方法的另一种替代方法(但同样的错误仍然存在):
$.ajax({ url: '/users/' + someUserId, type: 'DELETE', success: function(result) { // Do something with the result } });