zoukankan      html  css  js  c++  java
  • springmvc REST风格的URL

    1:需要配置一个filter

    <!-- 
        配置 org.springframework.web.filter.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>

    2:在页面上请求 PUT和DELETE都是有post转换的。放在隐藏域中

    <a href="first/testRest/1">testRest GET</a>
          <br><br><br>
      
          <form action="first/testRest/1" method="post">
            <input type="hidden" name="_method" value="PUT"/>
            <input type="submit" value="TestRest PUT"/>
        </form>
        <br><br>
        
        <form action="first/testRest/1" method="post">
            <input type="hidden" name="_method" value="DELETE"/>
            <input type="submit" value="TestRest DELETE"/>
        </form>
        <br><br>
        
        <form action="first/testRest" method="post">
            <input type="submit" value="TestRest POST"/>
        </form>
        <br><br>

    3:在java类中写对应的方法

        /**
         * Rest 风格的 URL. 以 CRUD 为例: 新增: /order POST 修改: /order/1 PUT update?id=1 获取:
         * /order/1 GET get?id=1 删除: /order/1 DELETE delete?id=1
         * 
         * 如何发送 PUT 请求和 DELETE 请求呢 ? 1. 需要配置 HiddenHttpMethodFilter 2. 需要发送 POST 请求
         * 3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
         * 
         * 在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解
         * 
         */
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
        public String testRestPut(@PathVariable Integer id) {
            System.out.println("testRest Put: " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
        public String testRestDelete(@PathVariable Integer id) {
            System.out.println("testRest Delete: " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest", method = RequestMethod.POST)
        public String testRest() {
            System.out.println("testRest POST");
            return SUCCESS;
        }
    
        @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
        public String testRest(@PathVariable Integer id) {
            System.out.println("testRest GET: " + id);
            return SUCCESS;
        }
  • 相关阅读:
    天使投资人如何评估创业公司价值
    web报表工具finereport常用函数的用法总结(数组函数)
    采用UltraISO制作U盘启动盘
    web报表工具FineReport常用函数的用法总结(报表函数)
    web报表工具FineReport使用中遇到的常见报错及解决办法(三)
    Dll的编写 在unity中加载
    Unity 实现模拟按键
    web报表工具FineReport最经常用到部分函数详解
    Unity UGUI
    带有天气预报的高大上web报表制作分享
  • 原文地址:https://www.cnblogs.com/bulrush/p/8029787.html
Copyright © 2011-2022 走看看