1、前端代码
<!--1 给当前按钮绑定样式deleteBtn
2 给按钮绑定自定义属性-->
<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn"
type="submit">删除</button>
<!--创建一个表单-->
<form id="delFormBtn" method="post">
<input type="hidden" name="_method" value="delete">
</form>
<script>
// 按钮点击时调用该方法
$(".deleteBtn").click(function(){
//绑定属性提交表单
$("#delFormBtn").attr("action",$(this).attr("del_uri")).submit();
return false;
});
</script>
2、服务端代码
@DeleteMapping("/emp/{id}")
public String delete(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
3、Spring boot源码(重点)
3.1、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()的条件。
4、配置文件
在application.yml或者application.properties中加入spring.mvc.hiddenmethod.filter.enabled = true即可。