springboot接收浏览器发送delete请求( method not allowed 405解决方法)
【README】
瀏覽器使用form提交信息的時候只支持GET和POST,如果需要在瀏覽器上使用PUT和DELETE請求方式的話,只能使用欺騙的方式了,SpringMvc提供了HiddenHttpMethodFilter類來提供支持;
【1】前端
1)list.html
<body><!-- 引入抽取的topbar --><!--模板名: 會使用 thymeleaf的前后綴配置規則進行解析 --><!--<div th:replace="~{dashboard::topbar}"></div-->><div th:replace="commons/bar::topbar"></div><div class="container-fluid"><div class="row"><!-- 引入側邊欄 --><!--<div th:replace="~{dashboard::#sidebar}"></div>--><div th:replace="commons/bar::#sidebar(activeUri='emps')"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2><a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">員工添加</a></h2><div class="table-responsive"><table class="table table-striped table-sm"><thead><tr><td>id</td><td>lastName</td><td>email</td><td>gender</td><td>department</td><td>birth</td><td>操作</td></tr></thead><tbody><tr th:each="emp:${emps}"><td th:text="${emp.id}"></td><td>[[${emp.lastName}]]</td><td>[[${emp.email}]]</td><td th:text="${emp.gender=='0'?'女':'男'}"></td><td th:text="${emp.department.departmentName}"></td><td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td><td><a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">編輯</a><button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">刪除</button></td></tr></tbody></table></div></main><form id="deleteEmpForm" method="post"><input type="hidden" name="_method" value="delete" /></form></div> </div>其中刪除發送的是 rest風格的delete請求;
<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">刪除</button><form id="deleteEmpForm" method="post"><input type="hidden" name="_method" value="delete" /> </form><script>$(".deleteBtn").click(function(){// 刪除當前員工 $("#deleteEmpForm").attr("action",$(this).attr("del_uri")).submit();return false;}); </script>【2】springboot后端
EmployeeController 控制器
@Controller public class EmployeeController {@AutowiredEmployeeDao employeeDao;@AutowiredDepartmentDao departmentDao;// 查詢所有員工返回列表頁面@GetMapping(value="/emps")public String list(Model model) {Collection<Employee> employees = employeeDao.getAll();// 放在請求域中model.addAttribute("emps", employees);// thymeleaf 默認拼串// classpath:/templates/XXXX.htmlreturn "emp/list";}// 來到員工添加頁面@GetMapping("/emp")public String toAddPage(Model model) {// 來到添加頁面, 查詢所有部門,在頁面顯示Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("depts", departments);return "emp/add";}// 員工添加功能,springmvc自動將請求參數和入參對象的屬性進行一一綁定 ,請求參數名字和javaBean入參屬性名是一致的@PostMapping("/emp")public String addEmp(Employee employee) {employeeDao.save(employee);/*** 添加成功后,來到員工列表頁面,emp/list.html,有兩種方式:* 方式1,redirect:/emps 重定向;方式2,forward: /emps 請求轉發;* 不能直接返回 /emps,因為thymeleaf模板引擎會解析為 emps.html*/return "redirect:/emps";}// 來到員工修改頁面, 查出當前員工,在頁面回顯@GetMapping("/emp/{id}")public String toEditPage(@PathVariable("id") Integer id, Model model) {// 頁面顯示所有部門列表model.addAttribute("depts", departmentDao.getDepartments());// 查詢員工model.addAttribute("emp", employeeDao.get(id));// 回到修改或編輯頁面(add是新增或編輯頁面)return "emp/add";}// 員工修改請求@PutMapping("/emp")public String toEditPage(Employee employee) {employeeDao.save(employee);return "redirect:/emps";}// 員工刪除請求@DeleteMapping("/emp/{id}")public String deleteEmployee(@PathVariable("id") Integer id) {employeeDao.delete(id);return "redirect:/emps";} }其中處理刪除請求的映射如下:
// 員工刪除請求,接收delete請求@DeleteMapping("/emp/{id}")public String deleteEmployee(@PathVariable("id") Integer id) {employeeDao.delete(id);return "redirect:/emps";}【3】點擊刪除
報錯:(type=Method Not Allowed, status=405?
?method not allowed 405,表示 服務器不接收delete請求;
原因: springboot 沒有啟用 HiddenHttpMethodFilter 過濾器來支持delete請求;
解決方法:在 application.properties 中啟用該過濾器, 如下:
application.properties # 啟動HiddenHttpMethodFilter過濾器,以支持瀏覽器可以發送DELETE PUT 請求 spring.mvc.hiddenmethod.filter.enabled=true?重啟后,再次訪問,如下:
?【4】附錄:HiddenHttpMethodFilter
javax.servlet.Filter 將發布的方法參數轉換為 HTTP 方法,可通過 HttpServletRequest.getMethod() 檢索。 由于瀏覽器目前僅支持 GET 和 POST,因此一種常用技術(例如 Prototype 庫使用的技術)是使用帶有附加隱藏表單字段 (_method) 的普通 POST 來傳遞“真正的”HTTP 方法。 此過濾器讀取該參數并相應地更改 HttpServletRequestWrapper.getMethod() 返回值。 只允許使用“PUT”、“DELETE”和“PATCH”HTTP 方法。
請求參數的名稱默認為 _method,但可以通過 methodParam 屬性進行調整。
總結
以上是生活随笔為你收集整理的springboot接收浏览器发送delete请求( method not allowed 405解决方法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 与亲妈聊天时无话可说和爸妈没话聊
- 下一篇: 教你如何用一套键鼠控制两台电脑教你如何用