Matrix Variables
来源:互联网 发布:mac系统序列号覆盖 编辑:程序博客网 时间:2024/06/05 20:48
The URI specification RFC 3986 definesthe possibility of including name-value pairs within path segments. There is no specificterm used in the spec. The general "URI path parameters" could be applied although themore unique"Matrix URIs", originatingfrom an old post by Tim Berners-Lee, is also frequently used and fairly well known.Within Spring MVC these are referred to as matrix variables.
Matrix variables can appear in any path segment, each matrix variable separated with a";" (semicolon). For example:"/cars;color=red;year=2012"
. Multiple values may beeither "," (comma) separated"color=red,green,blue"
or the variable name may berepeated"color=red;color=green;color=blue"
.
If a URL is expected to contain matrix variables, the request mapping pattern mustrepresent them with a URI template. This ensures the request can be matched correctlyregardless of whether matrix variables are present or not and in what order they areprovided.
Below is an example of extracting the matrix variable "q":
// GET /pets/42;q=11;r=22@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)public void findPet(@PathVariable String petId, @MatrixVariable int q) { // petId == 42 // q == 11}
Since all path segments may contain matrix variables, in some cases you need to be morespecific to identify where the variable is expected to be:
// GET /owners/42;q=11/pets/21;q=22@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)public void findPet( @MatrixVariable(value="q", pathVar="ownerId") int q1, @MatrixVariable(value="q", pathVar="petId") int q2) { // q1 == 11 // q2 == 22}
A matrix variable may be defined as optional and a default value specified:
// GET /pets/42@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)public void findPet(@MatrixVariable(required=false, defaultValue="1") int q) { // q == 1}
All matrix variables may be obtained in a Map:
// GET /owners/42;q=11;r=12/pets/21;q=22;s=23@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)public void findPet( @MatrixVariable Map<String, String> matrixVars, @MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) { // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23] // petMatrixVars: ["q" : 11, "s" : 23]}
Note that to enable the use of matrix variables, you must set theremoveSemicolonContent
property ofRequestMappingHandlerMapping
to false
. Bydefault it is set to true
.
The MVC Java config and the MVC namespace both provide options for enabling the use ofmatrix variables.
If you are using Java config, The Advanced Customizationswith MVC Java Config section describes how the RequestMappingHandlerMapping
canbe customized.
In the MVC namespace, the <mvc:annotation-driven>
element has anenable-matrix-variables
attribute that should be set totrue
. By default it is setto false
.
<?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>
- Matrix Variables
- Variables
- Variables
- Variables
- variables
- Variables
- python global variables,member variables,instance variables
- Using Variables
- Environment Variables
- Indicator Variables
- Swapping variables.
- JavaScript Variables
- VBScript Variables
- condition variables
- C++ Variables
- Local Variables
- Condition Variables
- Auto Variables
- 如何用纯代码构建一个 Widget(2014)
- Spring框架入门笔记——深入了解Spring容器的Bean
- 串的模式匹配算法(BF算法和KMP算法)
- postgreSQL 终止正在执行的SQL
- zepto判断左右滑动
- Matrix Variables
- stl源码剖析 详细学习笔记stack queue
- 一个球从距地面100米的地方落下,每次落地后反弹起跳高度为原来的一半,经过10次落地,共经过多少米?第10次反弹多高?
- 【猫猫的Unity Shader之旅】之Unity Shader概述及学习环境搭建
- opencv笔记:K-mean
- 最大连续和2——dp
- 1个键盘输入处理的例子:GetKeyState
- mongodb 数据库连接
- HDU 5186 || 模拟map