restful风格 PUT方式提交表单

  作者:记性不好的阁主

配置开启restful


#开启restful
spring.mvc.hiddenmethod.filter.enabled=true


1、前端先定义form表单方式为post


2、再在表单中定义一个_method="PUT"的隐藏输入框



<!--需要区分是员工修改还是添加;-->
<form th:action="@{/emp}" method="post">
<!--发送put请求修改员工数据-->
<!--
1SpringMVC中配置HiddenHttpMethodFilter;SpringBoot自动配置好的)
2、页面创建一个post表单
3、创建一个input项,name="_method";值就是我们指定的请求方式
-->
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
</div>
<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
</form>



控制器接收put请求


@PutMapping("/emp")
public String editEmp(Employee employee){
employeeDao.save(employee);
return "redirect:emp/list";
}


DELETE方式同理


相关推荐

评论 抢沙发

表情

分类选择