[英]What are the advantages of @MatrixVariable over @RequestParam?
我在本文中閱讀http://www.kscodes.com/spring-mvc/spring-mvc-matrix-variable-example/的一個好處是,您可以將變量類型Map
用作矩陣變量,並且您可以使用@RequestParam
時,請使用此類型。 但是除此之外,還有其他原因為什么我應該使用@MatrixVariable
而不是@RequestParam
?
謝謝
與@RequestParam
不同, @RequestParam
@MatrixVariable
由分號分隔;
和多個值由逗號分隔,
。 閱讀其文檔:
指示方法參數應綁定到路徑段內的名稱/值對的注釋。 支持帶注釋的RequestMapping處理程序方法。
有很多示例和用法變體。 這里有一些例子:
網址: localhost:8080/person/Tom;age=25;height=175
and Controller:
@GetMapping("/person/{name}") @ResponseBody public String person( @PathVariable("name") String name, @MatrixVariable("age") int age, @MatrixVariable("height") int height) { // ... }
它甚至可以映射到String[]
。
URL: localhost:8080/person/Tom;languages=JAVA,JAVASCRIPT,CPP,C
和控制器
public String person( @PathVariable("name") String name, @MatrixVariable("languages") String[] languages) { // ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.