zoukankan      html  css  js  c++  java
  • HiddenHttpMethodFilter进行请求过滤

    基于 HiddentHttpMethodFilter 的示例

    作用:
    由于浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE、PUT 等 method 并不支持,Spring3.0 添
    加了一个过滤器,可以将浏览器请求改为指定的请求方式,发送给我们的控制器方法,使得支持 GET、POST、PUT
    与 DELETE 请求。
    使用方法:
    第一步:在 web.xml 中配置该过滤器。
    第二步:请求方式必须使用 post 请求。
    第三步:按照要求提供_method 请求参数,该参数的取值就是我们需要的请求方式。
    源码分析:

    HiddenHttpMethodFilter进行请求过滤,实现Rest风格的url
    Rest 风格的 URL.
    以 CRUD 为例:
    新增: /order POST
    修改: /order/1 PUT update?id=1
    获取:/order/1 GET get?id=1
    删除: /order/1 DELETE delete?id=1、

    浏览器只支持Post和get的方式,想要实现delete和put的方式,需要使用过滤器HiddenHttpMethodFilter

    1、web.xml配置过滤

    <filter>
            <filter-name>hidden</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>hidden</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    

    2,在客户端发起请求
    过滤器使用_method的这个参数来决定过滤成是什么类型的,因此,需要在前端的提交表单里面加上_method的隐藏域,注意要使用post方法进行提交

    <form action="/rest/12" method="post">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="delete">
      </form>
      <form action="/rest/12" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="put">
      </form>
      <form action="/rest/12" method="post">
        <input type="submit" value="post">
      </form>
      <form action="/rest/12" method="get">
        <input type="submit" value="get">
      </form>
    

    3,后端控制器的编写

    控制器直接使用RequestMapping来指定方法就可以了

    @RequestMapping(value = "/rest/{id}",method = RequestMethod.DELETE)
        public String testrestDELETE(@PathVariable int id, Model model){
            model.addAttribute("msg","delete请求"+id);
            return SUCCESS;
        }
        @RequestMapping(value = "/rest/{id}",method = RequestMethod.PUT)
        public String testrestPUT(@PathVariable int id,Model model){
            model.addAttribute("msg","put请求"+id);
            return SUCCESS;
        }
        @RequestMapping(value = "/rest/{id}",method = RequestMethod.POST)
        public String testrestPOST(@PathVariable int id,Model model){
            model.addAttribute("msg","post请求"+id);
            return  SUCCESS;
    
        }
        @RequestMapping(value = "/rest/{id}",method = RequestMethod.GET)
        public String testrestDELETE(@PathVariable int id, ModelMap modelMap){
            modelMap.addAttribute("msg","get请求"+id);
            return SUCCESS;
        }
    

    特别注意!!!!!!!!!!!!!

    在tomcat8上面是不支持delete和post请求的,因此以上只能在tomcat7上面执行

    tomcat8运行时可以进入到相应的控制器,但是视图渲染返回的时候,由于不支持这两种方法,就会报出异常页面

  • 相关阅读:
    Spring MVC全局异常后返回JSON异常数据
    spring mvc 异常统一处理方式
    Duplicate fragment name ERROR Jetty Maven Plugin
    Android中自己定义组件和它的属性
    stl非变易算法(二)
    unix more命令
    g711u与g729比較编码格式
    MD5的加密和解密(总结)
    js 定义类对象
    润乾报表实现可反复分组报表及改进
  • 原文地址:https://www.cnblogs.com/zgrey/p/13395885.html
Copyright © 2011-2022 走看看