[英]HTTP Status 405 - Request method 'GET' not supported occurs when calling a delete request
I am trying to implement delete action for the each datatable row by invoking a bootstrap modal. 我正在尝试通过调用引导程序模式为每个数据表行实现删除操作。 And then sending a delete request to the controller with the parameter of username. 然后使用用户名参数向控制器发送删除请求。
$(document).ready(function () {
$('#usersTable').DataTable({
"sAjaxSource": "users/list",
"sAjaxDataProp": "",
"aoColumns": [
{"mData": "username"},
{"mData": "firstName"},
{"mData": "lastName"},
{"mData": "status"},
{"mData": "role.roleName"},
{
mRender: function (data, type, row) {
var linkDel = "<a href=\"#\" onclick=\"fillDeleteFrm(\'username\')\">Delete</a>";
linkDel = linkDel.replace("id", row.id);
linkDel = linkDel.replace("name", row.name);
return linkDel;
}
}
]
})
});
function fillDeleteFrm(username) {
$('#delete-username').val(username);
$('#userDeleteModal').modal('toggle');
return false;
}
The thymeleaf form that calls the spring controller : 称为弹簧控制器的百里香形式:
<div id="userDeleteModal">
<form id="systemUserForm" th:action="@{/userdelete}" th:method="delete">
<input type="hidden" id="delete-username" name="username" th:value="*{username}">
<button type="submit" class="btn btn-primary">Delete</button>
</div>
</form>
The corresponding spring controller : 对应的弹簧控制器:
@ResponseBody
@RequestMapping(value="/userdelete", method = RequestMethod.DELETE)
public ModelAndView deleteEmployee(@RequestParam String username) {
User user = userService.findUserByUserName(username);
userService.deleteUser(user);
return new ModelAndView("users");
}
Even though I call the delete request method the mapping does not route to this controller, alerting 即使我调用了delete request方法,映射也不会路由到该控制器,提醒
HTTP Status 405 - Request method 'GET' not supported
What would I have done wrong? 我做错了什么? Thanks in advance. 提前致谢。
Browsers do not support DELETE
method in forms. 浏览器不支持表单中的DELETE
方法。 With th:method="delete"
, Thymleaf adds a hidden _method form field. 通过th:method="delete"
,Thymleaf添加了一个隐藏的_method表单字段。
<input type="hidden" name="_method" value="delete">
If you add [HiddenHttpMethodFilter][1]
filter in your application, it will convert the post request to a delete request based on the _method parameter. 如果在应用程序中添加[HiddenHttpMethodFilter][1]
过滤器,它将根据_method参数将发布请求转换为删除请求。 Try adding the following in your web.xml file. 尝试在web.xml文件中添加以下内容。
If you are using spring boot, the filter is automatically included. 如果您使用的是弹簧靴,则将自动包含过滤器。 But in a plain spring web mvc app, you'll have to add the filter manually in web.xml file. 但是在普通的Spring Web MVC应用程序中,您必须在web.xml文件中手动添加过滤器。 This filter is probably missing in your app. 您的应用中可能缺少此过滤器。 That is why the GET
requested is not being converted to a DELETE
request. 这就是为什么未将请求的GET
转换为DELETE
请求的原因。
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.