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>

posted on 2019-09-05 20:56  kfsrex  阅读(131)  评论(0编辑  收藏  举报