最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用@pathvariable與@requestparam碰到的一些問題及解決

 更新時間:2021年08月17日 10:37:33   作者:feidao0  
這篇文章主要介紹了使用@pathvariable與@requestparam碰到的一些問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@pathvariable與@requestparam碰到的一些問題

一、@pathvariable

可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校篣RL 中的 {x} 占位符可以通過@PathVariable("x") 綁定到操作方法的入?yún)⒅小?/p>

@GetMapping("/test/{id}")
public String test(@PathVariable("id") String id){
    System.out.println("test:"+id);
    return SUCCESS;
}

可以看出使用@pathvariable注解它直接從url中取參,但是如果參數(shù)是中文就會出現(xiàn)亂碼情況,這時應(yīng)該使用@requestparam注解

二、@requestparam

它是直接從請求中取參,它是直接拼接在url后面(demo?name=張三)

@GetMapping("/demo")
public String test(@requestparam(value="name") String name){
     System.out.println("test:"+name);
     return SUCCESS;
}

注:如果參數(shù)不必須傳入的話,我們從源碼中可以看出兩者required默認(rèn)為true,如圖:

所以我們可以這樣寫,只寫一個例子

@GetMapping("/demo")
public String test(@requestparam(value="name", required = false) String name){
     System.out.println("test:"+name);
     return SUCCESS;
}

@PathVariable和@RequestParam的使用說明

要說明@PathVariable和@RequestParam的使用,首先介紹 @RequestMapping

RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。

RequestMapping:Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.Both Spring MVC and Spring WebFlux support this annotation.

RequestMapping注解有六個屬性,常用的是value,method;還有consumes,produces,params,headers。

value屬性:指定請求的實(shí)際地址,當(dāng)只設(shè)置value屬性時,默認(rèn)省略不寫

即:@RequestMapping("/hello")或@RequestMapping(value="/hello")

value的uri值為以下三類

  • A)可以指定為普通的具體值;
  • B)可以指定為含有某變量的值(URI Template Patterns with Path Variables);
  • C)可以指定為含正則表達(dá)式的值( URI Template Patterns with Regular Expressions)。

HelloController.java極簡代碼示例,既有PathVariable也有RequestParam

@RestController
public class HelloController {
 @RequestMapping("/hellopv/{name}")
 public String helloPV(@PathVariable String name, @RequestParam String username) {
  String hello = "Hello " + username + " [" + name + "] !";
  return hello;
 }
}

感性認(rèn)識一下,測試上述代碼http://cos6743:8081/hellopv/tom?username=YangTom

url

@PathVariable是處理requet uri template中variable 的注解,實(shí)現(xiàn)了url入?yún)⒔壎ǖ椒椒▍?shù)上。

即:可以獲取URL請求路徑中的變量值,比如:RequestMapping("/hellopv/{name}")中的name

@RequestParam獲取URL請求數(shù)據(jù),是常用來處理簡單類型的綁定注解。

通過Request.getParameter()獲取入?yún)?,故此可以處理url中的參數(shù),也可以處理表單提交的參數(shù)和上傳的文件。

拓展

handler method 參數(shù)綁定常用的注解,根據(jù)處理的Request的不同內(nèi)容分為四類常用類型

  • A、處理requet uri 部分(指uri template中variable)的注解: @PathVariable;
  • B、處理request header部分的注解: @RequestHeader, @CookieValue;
  • C、處理request body部分的注解:@RequestParam, @RequestBody;
  • D、處理attribute類型是注解: @SessionAttributes, @ModelAttribute;

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

都兰县| 池州市| 武山县| 黄大仙区| 巩留县| 扶余县| 吴忠市| 昌邑市| 怀柔区| 广水市| 五家渠市| 赣州市| 余庆县| 乌审旗| 南召县| 天水市| 黄大仙区| 屯昌县| 萨嘎县| 金平| 巴楚县| 西乌珠穆沁旗| 海丰县| 阳西县| 赣州市| 平定县| 台安县| 五峰| 仪陇县| 庆云县| 阜平县| 双江| 宜川县| 米脂县| 英山县| 旌德县| 库车县| 盐城市| 吉木乃县| 乐业县| 沧州市|