最近在学习一个springboot项目的时候,在获得@DeleteMapping请求时出错,页面显示Request method ‘POST’ not supported
controller层的代码
@DeleteMapping("/xx/{id}")
public String deleteEmployee(@PathVariable("id")Integer id){
user.delete(id);
return "redirect:/index";
}
表单代码
<form th:action="@{/xx/}+${user.id}" method="post">
<input type="hidden" name="_method" value="delete">
<button type="submit" >删除</button>
</form>
运行后出错
然后苦逼各种搜解决方法 都没成功,便翻看源码,如下:
matchIfMissing的用法:
(1)如果配置文件中没有配置 spring.mvc.hiddenmethod.filter.enabled 的属性,并且matchIfMissing = false,那么不符合加载
hiddenHttpMethodFilter()的条件,反之matchIfMissing = true,符合加载hiddenHttpMethodFilter()的条件。
(2)如果配置文件中配置了 spring.mvc.hiddenmethod.filter.enabled = false,不管matchIfMissing = false 或者 matchIfMissing = true,都不符合加载hiddenHttpMethodFilter()的条件。
(3)如果配置文件中配置了 spring.mvc.hiddenmethod.filter.enabled = true ,不管matchIfMissing = false 或者 matchIfMissing = true,都符合加载hiddenHttpMethodFilter()的条件。
所以得出解决办法:
在application.yml或者application.properties中加入spring.mvc.hiddenmethod.filter.enabled = true
即可