學前須知
這是關于String MVC架構的掃描注釋操作部分,感覺有點難以了解,可以先學習此架構的入門知識。
邁入正題
@MatrixVariable的使用簡單來說就是擷取-路徑中的變量和值,也就是通過路徑傳遞參數。
如下:
//在某路徑名中有如下這些内容:
/cars;color=red;year=2020 --兩個不同的變量用“;”分隔開
/cars;color=red,green,blue --同一個變量的多個數值用“,”分隔開
/cars;color=red;color=green;color=blue --也可以重複使用變量名
擷取路徑中參數的方式:
使用@MatrixVariable注釋類進行擷取。
需要配合使用的注釋類:
1、@RequestMapping:定義路徑
2、@PathVariable: 擷取路徑中“{ }”内變量的内容。
注意
使用@MatrixVariable前需要添加mvc配置,不然無法使用。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven enable-matrix-variables="true"/>
</beans>
主要就是添加上 語句
mvc命名空間的屬性知識檢視我的String MVC mvc命名空間屬性總結
開始擷取
簡單使用方式:
@RequestMapping(path = "/Demo2/{id}",method = RequestMethod.GET)
public String test1(@PathVariable String id,@MatrixVariable String color, @MatrixVariable int year){
System.out.println("id="+id+",color="+ color+",year="+year);
return "success";
}
<form action="/Demo2/66;color=red;year=2020" method="get">
<input type="submit" value="送出">
</form>
在頁面上點選送出之後結果:
浏覽器顯示位址樣式:
/Demo2/66;color=red;year=2020?
背景控制器顯示結果:
id=66,color=red,year=2020
多種使用方式:
/*擷取單個參數
* 請求路徑内容:/Demo2/66;color=red;year=2020 */
@RequestMapping(path = "/Demo2/{id}",method = RequestMethod.GET)
public String test1(@PathVariable String id,@MatrixVariable String color, @MatrixVariable int year){
System.out.println("id="+id+",color="+ color+",year="+year);
return "success";
}
/*擷取不同路徑段中的變量
* 請求路徑内容:/Demo2/66;q=45/pets/77;q=34*/
@RequestMapping(path = "/Demo2/{id1}/pets/{id2}" ,method = RequestMethod.GET)
public String test2(@MatrixVariable(name = "q",pathVar = "id1") int q1,
@MatrixVariable(name = "q",pathVar = "id2") int q2){
System.out.println("q1="+q1+",q2="+q2);
return "success";
}
/*設定預設值,請求段可以不需要設定參數
* 請求路徑内容:/Demo2/pets/66*/
@RequestMapping(path = "/Demo2/pets/{id}",method = RequestMethod.GET)
public String test3(@MatrixVariable(required = false,defaultValue = "1")int q){
System.out.println("q="+q);
return "success";
}
/*路徑中的所有變量都可以被Map獲得
* 請求路徑内容:/Demo2/66;q=45;s=54/pet/77;q=23;t=95*/
@RequestMapping(path = "/Demo2/{id1}/pet/{id2}",method = RequestMethod.GET)
public String test4(@MatrixVariable Map<String,String> map,
@MatrixVariable(pathVar = "id2") Map<String,String> mapid2){
System.out.print("map="+map.toString()+",mapid2="+mapid2.toString());
return "success";
}
對應表單:
<form action="/Demo2/66;color=red;year=2020" method="get">
<input type="submit" value="送出">
</form>
<form action="/Demo2/66;q=45/pets/77;q=34" method="get">
<input type="submit" value="送出">
</form>
<form action="/Demo2/pets/66" method="get">
<input type="submit" value="送出">
</form>
<form action="/Demo2/66;q=45;s=54/pet/77;q=23;t=95" method="get">
<input type="submit" value="送出">
</form>
運作結果
背景控制器顯示結果:
id=66,color=red,year=2020
q1=45,q2=34
q=1
map={q=45, s=54, t=95},mapid2={q=23, t=95}
關于@MatrixVariable的參數
屬性 | 屬性值 | 含義 |
---|---|---|
name | 設定要擷取路徑中某參數的參數名 | |
pathVar | 設定要擷取參數的路徑段 | |
value | 指定pathVar中的某參數名 | |
required | false | 路徑中可以沒有這個變量 |
true | 路徑中必須有這個變量預設 | |
defaultValue | 設定預設值 |
雲中君後話:
URI規範RFC 3986定義了在路徑段中包括名稱/值對的可能性,可以使用一般的“ URI路徑參數”,盡管也經常使用并且相當知名的,更獨特的“矩陣URI”。在Spring MVC中,這些被稱為矩陣變量。