Spring MVC 请求参数获取

2015-07-24  本文已影响320人  陈岳陵

GET

  1. 请求 默认的 Content-Type = "application/x-www-form-urlencoded"
    所以 当Spring 使用
    @RequestMapping(value = "/pets", method=RequestMethod.GET,consumes = "application/x-www-form-urlencoded")
    //进行注解时不会返回415 错误[UNSUPPORTED_MEDIA_TYPE]。

  2. 参数直接放在方法里
    @RequestMapping(value = "/pets", method =RequestMethod.GET,consumes = "application/x-www-form-urlencoded" )
    public void getPet(String id, Model model) {
    System.out.println(id);
    }
    //请求参数 localhost:8080/pets?id=1
    //打印结果 1

  3. 含有参数的对象放在方法里

    @RequestMapping(value = "/pets", method =RequestMethod.GET,consumes = "application/x-www-form-urlencoded" )                              
      public void getPet(PetForm dataForm, Model model) {       
             System.out.println(dataForm.getId());
      }
    //请求参数 localhost:8080/pets?id=1
    //打印结果 1
    
上一篇 下一篇

猜你喜欢

热点阅读