RESTful介绍
REST:即Representational State Transfer ,(资源)表现层状态转化,是目前最流行的一种互联网软件架构。
具体说,就是HTTP协议里面,四个表示操作方式的动词:
GET POST PUT DELETE
它们分别代表着四种基本操作:
- GET用来获取资源
- POST用来创建新资源
- PUT用来更新资源
- DELETE用来删除资源
示例:
- /order/1 HTTP GET :得到id = 1 的 order
- /order/1 HTTP DELETE: 删除 id=1 的order
- /order/1 HTTP PUT : 更新id = 1的 order
- /order HTTP POST : 新增 order
web.xml添加HiddenHttpMethodFilter配置
<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>
测试
需要注意: 由于doFilterInternal方法只对method为post的表单进行过滤,所以在页面中必须如下设置:
<form action="..." method="post">
<input type="hidden" name="_method" value="put" />
</form>
test.jsp
<h1> <a href="order/1">get</a><br></h1>
<h1> post:<form action="order/1" method="post">
<input type="submit" value="post">
</form></h1>
<h1> delete : <form action="order/1" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="delete">
</form>
</h1>
<h1> <span onclick="a()">p ut</span>
<form action="order/1" method="post" id="myform">
<input type="hidden" name="_method" value="put">
</form></h1>
<script type="text/javascript">
function a(){
var form2=document.getElementById("myform");
form2.submit();
}
</script>
test.java
@Controller
public class OrderController {
@RequestMapping(value = "order/{id}",method = RequestMethod.GET)
public String get(@PathVariable("id") int id){
System.out.println("get......");
return "get";
}
@RequestMapping(value = "order/{id}",method = RequestMethod.POST)
public String post(@PathVariable("id") int id){
System.out.println("post......");
return "post";
}
@RequestMapping(value = "order/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") int id){
System.out.println("delete......");
return "delete";
}
@RequestMapping(value = "order/{id}",method = RequestMethod.PUT)
public String put(@PathVariable("id") int id){
System.out.println("put......");
return "put";
}
}