- 不说那么多介绍了,什么restful风格url什么的!直接开始。
- 在web.xml文件中配置
<!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->
<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>
3.在springMvc配置文件中配置静态资源
<!--静态资源交给默认的Servlet-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
- 将Get请求转为Delete请求
<!--一个删除链接,为get请求-->
<a class="delete" href="emp/${emp.id}">Delete</a>
<!--利用js,将get请求转为DELETE请求-->
<script type="text/javascript">
$(function() {
alert("hello");
$(".delete").click(function(){
var href=$(this).attr("href");
$("form").attr("action",href).submit();
return false;
});
});
</script>
<!--DELETE请求细节-->
<form action="" method="POST">
<input type="hidden" name="_method" value="DELETE" >
</form>
<!--接收DELETE请求的细节-->
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
System.out.print(employeeDao.delete(id));
return "redirect:/emps";
}
5.Post请求转为Put请求
<form:form action="${pageContext.request.contextPath}/emp" method="POST"
modelAttribute="employee"><br>
<c:if test="${employee.id == null }">
<!-- path 属性对应 html 表单标签的 name 属性值 -->
LastName: <form:input path="lastName"/>
<form:errors path="lastName"></form:errors>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<!-- 将POST请求转化为PUT请求 -->
<input type="hidden" name="_method" value="PUT"/>
</c:if>
<br>
Email: <form:input path="email"/>
<input type="submit" value="Submit"/>
</form:form>