一.在http协议里面,有四种基本操作(请求方式)
1.GET
2.POST
3.PUT
4.DELETE
二.REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用。
通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:
/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1 HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据
三.将POST请求转为put和delete请求
需要在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>
四.java代码
四种请求:
@RequestMapping(value="testPost",method=RequestMethod.POST)
public String welcome11() {
System.out.println("testPost");
return "success3";
}
@RequestMapping(value="testDelete/{name}",method=RequestMethod.DELETE)
public String welcome12(@PathVariable("name") Integer id) {
System.out.println("testDelete"+id);
return "success3";
}
@RequestMapping(value="testPut/{name}",method=RequestMethod.PUT)
public String welcome13(@PathVariable("name") Integer id) {
System.out.println("testPut"+id);
return "success3";
}
@RequestMapping(value="testGet/{name}",method=RequestMethod.GET)
public String welcome14(@PathVariable("name") Integer id) {
System.out.println("testGet"+id);
return "success3";
}
五.jsp代码:
在表单域中需要携带一个name值为_method,value值为put或者delete的参数
-----------------------
<form action="handler/testPost/" method="post">
<input type="submit" value="增">
</form>
<form action="handler/testDelete/1234" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删">
</form>
<form action="handler/testPut/1234" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="改">
</form>
<form action="handler/testPost/1234" method="get">
<input type="submit" value="查">
</form>
-----------------------