SpringMVC的controller提供了PUT和DELETE的请求方式

来源:互联网 发布:java获取进程端口号 编辑:程序博客网 时间:2024/06/08 14:11

在Restful风格中,现有规定如下:

  1. GET(SELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。  
  2. POST(CREATE):在服务器新建一个资源,调用insert操作。  
  3. PUT(UPDATE):在服务器更新资源,调用update操作。  
  4. DELETE(DELETE):从服务器删除资源,调用delete语句
了解这个风格定义以后,我们举个例子:

如果当前url是 http://localhost:8080/User

那么用户只要请求这样同一个URL就可以实现不同的增删改查操作,例如

  1. http://localhost:8080/User?_method=get&id=1001  这样就可以通过get请求获取到数据库 user表里面 id=1001 的用户信息  
  2. http://localhost:8080/User?_method=post&id=1001&name=zhangsan  这样可以向数据库user 表里面插入一条记录  
  3. http://localhost:8080/User?_method=put&id=1001&name=lisi  这样可以将 user表里面 id=1001 的用户名改为lisi  
  4. http://localhost:8080/User?_method=delete&id=1001  这样用于将数据库user 表里面的id=1001 的信息删除 
这样定义的规范我们就可以称之为restful风格的API接口,我们可以通过同一个url来实现各种操作


在springMVC中实现restful风格开发

这里,我通过访问http://127.0.0.1:8080/ssmvc/restful接口的method不同来进入不同的controller方法,并打印返回数据。


  1. @RequestMapping(value = "/restful",method = RequestMethod.GET)  
  2.     public void list(HttpServletRequestrequest,HttpServletResponse response,TestVo vo) throws IOException {  
  3.        System.out.println("list被访问,参数:" + vo.toString());  
  4.        Map<String,Object> map= newHashMap<String, Object>();  
  5.        map.put("params",vo);  
  6.        map.put("method",RequestMethod.GET);  
  7.        response.getWriter().write(JSON.toJSONString(map));  
  8.     }  
  9.      
  10.     /** 
  11.      * restful风格insert接口 
  12.      * @param request 
  13.      * @param response 
  14.      * @param vo 
  15.      * @throws IOException 
  16.      */  
  17.     @RequestMapping(value = "/restful",method = RequestMethod.POST)  
  18.     public voidupdate(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {  
  19.        System.out.println("update被访问,参数:" + vo.toString());  
  20.        Map<String,Object> map= newHashMap<String, Object>();  
  21.        map.put("params",vo);  
  22.        map.put("method",RequestMethod.POST);  
  23.        response.getWriter().write(JSON.toJSONString(map));  
  24.     }  
  25.      
  26.     /** 
  27.      * restful风格update接口 
  28.      * @param request 
  29.      * @param response 
  30.      * @param vo 
  31.      * @throws IOException 
  32.      */  
  33.     @RequestMapping(value = "/restful",method = RequestMethod.PUT)  
  34.     public void add(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {  
  35.         System.out.println("add被访问,参数:" + vo.toString());  
  36.        Map<String,Object> map= newHashMap<String, Object>();  
  37.        map.put("params",vo);  
  38.        map.put("method",RequestMethod.PUT);  
  39.        response.getWriter().write(JSON.toJSONString(map));  
  40.     }  

  1. /** 
  2.      * restful风格delete接口 
  3.      * @param request 
  4.      * @param response 
  5.      * @param vo 
  6.      * @throws IOException 
  7.      */  
  8.     @RequestMapping(value = "/restful/{id}",method = RequestMethod.DELETE)  
  9.     public void del(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id) throws IOException {  
  10.         System.out.println("delete被访问,参数:"  + ", id:"+ id);  
  11.        Map<String,Object> map= newHashMap<String, Object>();  
  12.        map.put("params",id);  
  13.        map.put("method",RequestMethod.DELETE);  
  14.        response.getWriter().write(JSON.toJSONString(map));  
  15.     }  

这里要注意一下

1.html表单form中,method没有put、delete。

2.springMVC并不能直接接收到通过put、delete方式传过来的参数。

我这里的解决方式是

1.添加过滤器hiddenHttpMethodFilter,作用是将put和delete的参数获取并重新放入request中,controller便可以直接拿到这些参数。

我们需要在web.xml中配置一个过滤器

<!-- 配置过滤器 将POST请求转换为PUT和DELETE请求 -->
  <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>

需要注意的是,只有context-type:application/x-www-form-urlencoded的请求才会被过滤。


在表单上增加一个隐藏表单域,将HiddenHttpMethodFilter里的_method属性改为put或者delete后提交
因为只有form表单才具有post方法,而这个过滤器也只能将post方法转化,get则不行。
比如你要提交一个删除的请求
其Controller里的方法对应的
@RequestMapping(value="deleteById/{id}",method = RequestMethod.DELETE)
 
//RESTFUL风格
<a class="del" href="deleteById/100">DELETE</a>//将ID为100的删除
 
<form action="" method="post" id="delForm">
    <input type="hidden" name="_method" value="DELETE">
</form>
当然这种提交是要写js的
$(function(){
    $('.del').click(function(){
        $('#delForm').attr('action',this.href).submit();
    })
})
这段js脚本的意思就是
在点击<a>标签删除的时候将a标签的href赋值给id为delForm的表单的action然后让这个form
表单提交.这就完成了将POST请求转换成DELETE请求,那么PUT请求也可以同样这样做.

阅读全文
0 0