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

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

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

@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)亂碼情況,這時(shí)應(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,如圖:

所以我們可以這樣寫,只寫一個(gè)例子

@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是一個(gè)用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(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注解有六個(gè)屬性,常用的是value,method;還有consumes,produces,params,headers。

value屬性:指定請求的實(shí)際地址,當(dāng)只設(shè)置value屬性時(shí),默認(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;

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

相關(guān)文章

最新評論

黎川县| 仁化县| 三江| 万盛区| 米林县| 扬州市| 麻栗坡县| 蓬安县| 都江堰市| 雷波县| 柳江县| 嘉义县| 鄂尔多斯市| 辉南县| 班玛县| 镇原县| 汉川市| 永川市| 晋宁县| 福鼎市| 安新县| 嘉黎县| 苍南县| 嘉峪关市| 广宗县| 江源县| 贺州市| 日喀则市| 瑞丽市| 卢氏县| 鄂托克前旗| 临邑县| 扬中市| 巨鹿县| 京山县| 新绛县| 康定县| 定南县| 高雄市| 寿光市| 阿坝|