zoukankan      html  css  js  c++  java
  • RESTful风格

    RESTful风格特点:

    1. 每一个URL代表1种资源。
    2. 哭护短使用get,post,put,delete标识CRUD,get用来获取资源,post用来新建资源,put用来更新资源。delete用来删除资源。
    3. 通过资源的表现形式来操作资源。

    RESTful的各种用法:

           /*

            * RESTful风格

            * user/1

            * value="{uid}":接收网页传的参数uid

            * method=RequestMethod.GET:处理get请求

            * @PathVariable("uid") String id:把uid的值赋给形参id

            *

            */

           @RequestMapping(value="{uid}",method=RequestMethod.GET)

           public ModelAndView selectById(@PathVariable("uid") String id) {//查询

                  ModelAndView mv=new ModelAndView("index");

                  System.out.println(id+"selectById");

                  return mv;

           }

           @RequestMapping(method=RequestMethod.POST)

           public ModelAndView insert(User user) {//添加

                  ModelAndView mv=new ModelAndView("index");

                  System.out.println(user+"insert");

                  return mv;

           }

           /*

            * 使用PUT,和DELETE是要先配置一个spring的将post转换为put或delete的过滤器

            * 在传参数时,加一个      _method:put(或delete):用来告诉spring真正的请求方式

            * 在Controller可以加@ResponseBody注解(这样就不会有405错误)

            * put基本是ajax,这个方法的返回类型是ModelAndView会报405

            */

           @RequestMapping(method=RequestMethod.PUT)

           @ResponseBody

           public String update(User user) {//更新

                  System.out.println(user+"update");

                  return "index";

           }

           @RequestMapping(value="{uid}",method=RequestMethod.DELETE)

           @ResponseBody

           public String delete(@PathVariable("uid") String id) {//删除

                  System.out.println(id+"delete");

                  return "index";

           }

    springMVC配置文件中配置将post请求转换为put或delete请求的过滤器

      <!-- 该过滤器将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>

  • 相关阅读:
    内网很安全?错错错!附攻击演示
    Fiddler无所不能——之测试开发攻城狮必备利器
    【橙子独创】【假设前置数据异常法】案列解析
    偶发异常BUG,如何高效精准分析排查定位?
    史上最全提现模块案例分解
    移动端推送测试涉及点
    模拟导入系统通讯录5000+手机号 校验批量数据处理是否正常?
    发散逆向思维之查询类列表测试范围的思考
    PICT工具一键生成正交试验用例
    据说黑白红客大多是出身测试行业,那么前戏如何做好呢?戳下
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11469704.html
Copyright © 2011-2022 走看看