Spring @RequestParam注解的使用

1、@RequestParam作用

将请求参数绑定到你控制器的方法参数上

2、@RequestParam三个属性

@RequestParam(value=”参数名”, required=true/false, defaultValue=””):

(1)value:请求参数名(必须配置)单一参数时,可简化如下:

   /**
    * @Description:    url参数中的name必须要和@RequestParam("name")一致
    * @Author:         vdi100
    */
    @GetMapping("edit1")
    public String edit1(@RequestParam("userId") Integer userId, Model model) {
        System.out.println("*******************" + userId);
        model.addAttribute("userId" , userId);
        return "/admin/ronghe/rongheMobileList/edit";
    }
    /**
     * @Description:    url参数中的name必须要和@RequestParam("name")一致
     *                  参数名字不一样
     * @Author:         vdi100
     */
    @GetMapping("edit2")
    public String edit2(@RequestParam("userId") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)

   /**
    * @Description:    required = true时必须有参数id,否则会报错
    *                  required = false时参数id可不传,默认为null,所以此时参数类型不能为int
    * @Author:         vdi100
    */
    @GetMapping("edit3")
    public String edit3(@RequestParam(value = "id", required = true) Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false;如果没有传该参数,就使用默认值(可选配置)

   /**
    * @Description:    设置defaultValue值时,required无论设置是何值,都默认为false
    * @Author:         vdi100
    */
    @GetMapping("edit4")
    public String edit4(@RequestParam(value = "id", required = false, defaultValue = "10") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

3、@RequestParam接收前台传递过来的数组

如下图传递的参数是数组:

Spring @RequestParam注解的使用_第1张图片

@RequestParam接收参数:

   /**
    * @Description:    接收的参数ids是数组
    * @Author:         vdi100
    */
    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam(value = "ids[]", required = false) List ids) {
        if (null == ids) {
            return RestResponse.failure("ID不能为空" );
        }
        for (Long id : ids) {
            rongheMobileListService.deleteById(id);
        }
        return RestResponse.success();
    }

4、@RequestParam接收前台传递过来的Map

如下图前台ajax传递的参数data是一个Map

Spring @RequestParam注解的使用_第2张图片

@RequestParam接收参数:

   /**
    * @Description:    接收的参数data是Map
    * @Author:         vdi100
    */
    @PostMapping("entry")
    @ResponseBody
    public RestResponse add(@RequestParam(value = "data[mobile]") String mobile,
                            @RequestParam(value = "data[pro]") String pro,
                            @RequestParam(value = "data[realTime]", required = false) String realTime,
                            @RequestParam(value = "data[sjbm]", required = false) String sjbm,
                            @RequestParam(value = "data[sjmc]", required = false) String sjmc,
                            @RequestParam(value = "data[mobBm]", required = false) String mobBm,
                            @RequestParam(value = "data[mobMc]", required = false) String mobMc,
                            @RequestParam(value = "data[orderId]", required = false) String orderId,
                            @RequestParam(value = "data[sn]") String sn,
                            @RequestParam(value = "data[bakMobile]", required = false) String bakMobile) {

        return RestResponse.success();
    }

 

你可能感兴趣的:(Java,Spring)