SpringMVC註解 @Requestparam 和 @PathVariable 詳解

一、@Requestparam

  • 作用:綁定前端請求參數到對應的Controller層中方法的形參上
  • 註解參數:
    1. value:參數名稱,請求參數名必須與value一致,否則參數不會自動映射
    2. required:被標註參數是否必須包含;默認爲true,請求參數中必須包含被標註參數,否則會報錯,如果是false,請求參數可以不傳被標註參數,Boolean類型默認賦值爲false,其他類型默認賦值爲null;
    3. defaultValue:設置默認參數值,如果設置了該值,required=true將失效,自動爲false,如果沒有傳該參數,就使用所設置的默認值;
  • 示例:
 /**
     * @RequestParam()括號中什麼值都不寫的時候
     * value: 默認形參名 hello
     * required: 默認爲true 如果前端不傳值 程序會報錯
     * defaultValue:boolean默認賦值false,其他包裝類型默認賦值null
     * 注意:
     *      @RequestParam源碼中 name和 value 兩個屬性基本是等價的,都是獲取從前端傳入的參數
     */
    @RequestMapping("/test1")
    public void testRequestParam1(@RequestParam String hello){
    }

    /**
     * @RequestParam(value = "HELLO",required = false)
     * value: 前端請求參數名稱必須爲 HELLO,不一致則映射失敗
     * required: 此時前端不傳值,默認給HELLO賦值爲null
     * defaultValue:boolean默認賦值false,其他包裝類型默認賦值null
     *
     */
    @RequestMapping("/test2")
    public void testRequestParam2(@RequestParam(value = "HELLO",required = false) String hello){
    }

    /**
     * @RequestParam(value = "HELLO", defaultValue = "flag")
     * value: 前端請求參數名稱必須爲 HELLO,不一致則映射失敗
     * required: 因爲defaultValue設置了默認值,所以required=true失效,此時前端不傳HELLO不會報錯,默認賦值爲"flag"
     * defaultValue:如果前端傳值就原樣輸出,否則 HELLO = flag
     *
     */
    @RequestMapping("/test3")
    public void testRequestParam3(@RequestParam(value = "HELLO", defaultValue = "flag") String hello){
    }

二、@PathVariable

  • 作用:將 URL中的佔位符參數{xxx}綁定到Controller控制器處理方法的形參中;
  • 註解參數:
    1. value:參數名稱,需要跟URL中佔位符名稱一致;
    2. required:被標註參數是否必須包含;默認爲true,請求參數中必須包含被標註參數,否則會報錯,如果是false,請求參數可以不傳被標註參數,Boolean類型默認賦值爲false,其他類型默認賦值爲null(注意設置爲false,需要在@RequestMapping中配置多個請求路徑,示例Demo4);
  • 示例:

1、示例1(原始註解@PathVariable )

  • Demo1中URL中的佔位符名稱和Controller中方法形參一致,所以可以映射成功
  • Demo1示例:
@RestController
public class TestController {
   /**
     * Demo1
     * @PathVariable()
     * value: 默認形參名 hello
     * required: 默認爲true 如果前端不傳值 程序會報錯
     */
    @RequestMapping("/test4/{hello}/{world}")
    public String testPathVariable1(@PathVariable String hello, @PathVariable String world) {
        String str = "@PathVariable 佔位符映射-hello:"+ hello+"  world:"+world;
        return str;
    }
}
  • Demo1結果:
    在這裏插入圖片描述

2、示例2(URL佔位符名稱與Value名稱一致)

  • Demo2中URL佔位符名稱與Controller中方法形參名稱不一致,所以需要在@PathVariable中去聲明(@PathVariable中聲明名稱必須與URL佔位符名稱一致)
  • Demo2示例:
@RestController
public class TestController {
	/**
	  * Demo2
      * @PathVariable()
      * value: 默認形參名 hello
      * required: 默認爲true 如果前端不傳值 程序會報錯
      */
      @RequestMapping("/test5/{abc}/{efg}")
      public String testPathVariable2(@PathVariable("abc") String hello,  @PathVariable("efg") String world) {
     	 String str = "@PathVariable 佔位符映射-hello:"+ hello+"  world:"+world;
         return str;
    }
}

在這裏插入圖片描述

  • Demo2結果:
    在這裏插入圖片描述

3、示例3(URL佔位符名稱與Value名稱不一致)

  • Demo3中URL佔位符名稱與Controller中方法形參名稱不一致,並且@PathVariable中也沒有進行聲明,所以請求時會報錯;
  • Demo3示例:
@RestController
public class TestController {
   /**
   	 * Demo3
     * @PathVariable()
     * value: 默認形參名 hello
     * required: 默認爲true 如果前端不傳值 程序會報錯
     */
    @RequestMapping("/test6/{abc}/{efg}")
    public String testPathVariable3(@PathVariable String hello, @PathVariable String world) {
        String str = "@PathVariable 佔位符映射-hello:"+ hello+"  world:"+world;
        return str;
    }
}
  • Demo3結果:
    在這裏插入圖片描述

4、示例4(required = false)

  • Demo4中主要是測試required = false時,配置多個請求映射路徑;
  • Demo4示例:
@RestController
public class TestController {
   /**
   	 * demo4
     * @PathVariable(value = "abc",required = false)
     * value: 跟URL佔位符名稱一致
     * required: 默認爲true,如果前端不傳值程序會報錯,如果設置爲false需要配置多個請求路徑
     */
    @RequestMapping(value = {"/test7/{abc}/{efg}","/test7/{abc}","/test7"})
    public String testPathVariable4(@PathVariable(value = "abc",required = false) String hello,
                                    @PathVariable(value = "efg",required = false) String world) {
        String str = "@PathVariable 佔位符映射-hello:"+ hello+"  world:"+world;
        return str;
    }
}
  • Demo4結果:
    在這裏插入圖片描述

5、示例5(正則表達式規範請求URL)

  • Demo5中主要測試在@RequestMapping()的URL中配置正則表達式規範請求參數格式;
  • Demo5示例:
@RestController
public class TestController {
   /**
     *
     * @RequestMapping(value = {"/test8/{abc:^[a-zA-Z]+$}/{efg}","/test8/{abc:^[a-zA-Z]+$}","/test8"}
     * 正則表達式:^[a-zA-Z]+$  //只能是字母
     * @PathVariable(value = "abc",required = false)
     * value: 默認形參名 hello
     * required: 默認爲true 如果前端不傳值 程序會報錯
     */
    @RequestMapping(value = {"/test8/{abc:^[a-zA-Z]+$}/{efg}","/test8/{abc:^[a-zA-Z]+$}","/test8"})
    public String testPathVariable5(@PathVariable(value = "abc",required = false) String hello,
                                    @PathVariable(value = "efg",required = false) String world) {
        String str = "@PathVariable 佔位符映射-hello:"+ hello+"  world:"+world;
        return str;
    }

}
  • Demo5結果:
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章