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>

  • 相关阅读:
    洛谷P1306 斐波那契公约数
    Codevs 1688 求逆序对(权值线段树)
    poj1006 Biorhythms
    2017-9-2 NOIP模拟赛
    洛谷P1633 二进制
    洛谷P2513 [HAOI2009]逆序对数列
    洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower
    洛谷P2285 [HNOI2004]打鼹鼠
    2017-8-31 NOIP模拟赛
    洛谷P2134 百日旅行
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11469704.html
Copyright © 2011-2022 走看看