# Spring `@MatrixVariable`教程
> 原文: [http://zetcode.com/spring/matrixvariable/](http://zetcode.com/spring/matrixvariable/)
Spring `@MatrixVariable`教程展示了如何使用`@MatrixVariable`解析 URL 参数。
Spring 是用于创建企业应用的流行 Java 应用框架。
## `@MatrixVariable`
`@MatrixVariable`用于解析路径段中的名称-值对,并将它们绑定到方法参数。 多对用分号分隔。 必须首先启用矩阵变量。
## Spring `@MatrixVariable`示例
以下应用解析 URL 路径段中的名称/值对。
```java
pom.xml
src
├───main
│ ├───java
│ │ └───com
│ │ └───zetcode
│ │ ├───config
│ │ │ MyWebInitializer.java
│ │ │ WebConfig.java
│ │ └───controller
│ │ MyController.java
│ ├───resources
│ └───webapp
│ index.html
└───test
└───java
```
这是项目结构。
`pom.xml`
```java
4.0.0
com.zetcode
matrixvariableex
1.0-SNAPSHOT
war
UTF-8
11
11
5.1.3.RELEASE
javax.servlet
javax.servlet-api
4.0.1
provided
org.springframework
spring-webmvc
${spring-version}
org.apache.maven.plugins
maven-war-plugin
3.2.2
org.eclipse.jetty
jetty-maven-plugin
9.4.14.v20181114
```
在`pom.xml`文件中,我们具有项目依赖项。
`com/zetcode/config/MyWebInitializer.java`
```java
package com.zetcode.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@Configuration
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return null;
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
```
`MyWebInitializer`初始化 Spring Web 应用。 它包含一个配置类:`WebConfig`。
`com/zetcode/config/WebConfig.java`
```java
package com.zetcode.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.zetcode"})
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
var urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
```
`WebConfig`配置 Spring Web 应用。
```java
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
var urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
```
在这里,我们启用矩阵变量。
`com/zetcode/controller/MyController.java`
```java
package com.zetcode.controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class MyController {
@GetMapping(value = "/user/{first}/{last}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler1(@MatrixVariable("first") String first,
@MatrixVariable("last") String last) {
return String.format("Hello %s %s", first, last);
}
@GetMapping(value = "/data/{user:.*}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler2(@MatrixVariable Map data) {
return String.format("Id: %s\nFirst name: %s\nLast Name: %s\nEmail: %s\n",
data.get("id"), data.get("first"), data.get("last"), data.get("email"));
}
@GetMapping(value = "/geo/{continent}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler3(@PathVariable("continent") String continent,
@MatrixVariable("country") String country,
@MatrixVariable("capital") String capital) {
return String.format("Continent: %s\nCountry: %s\nCapital: %s\n",
continent, country, capital);
}
}
```
`MyController`包含请求路径到处理器方法的映射。
```java
@GetMapping(value = "/user/{first}/{last}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler1(@MatrixVariable("first") String first,
@MatrixVariable("last") String last) {
return String.format("Hello %s %s", first, last);
}
```
在这里,我们使用`@MatrixVariable`将多个矩阵变量绑定到方法参数。
```java
@GetMapping(value = "/data/{user:.*}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler2(@MatrixVariable Map data) {
return String.format("Id: %s\nFirst name: %s\nLast Name: %s\nEmail: %s\n",
data.get("id"), data.get("first"), data.get("last"), data.get("email"));
}
```
在这里,我们将多个名称/值对映射到一个映射中。
```java
@GetMapping(value = "/geo/{continent}",
produces = MediaType.TEXT_PLAIN_VALUE)
public String handler3(@PathVariable("continent") String continent,
@MatrixVariable("country") String country,
@MatrixVariable("capital") String capital) {
return String.format("Continent: %s\nCountry: %s\nCapital: %s\n",
continent, country, capital);
}
```
在第三种情况下,我们将`@MatrixVariable`与`@PathVariable`结合在一起。
`webapp/index.html`
```java
Home page
Greet user
Show user data
Show country info
```
这是主页。 我们有三个链接,这些链接包含使用`@MatrixVariable`注解解析的名称/值对。
在本教程中,我们使用`@MatrixVariable`解析路径段上的名称/值对并将其绑定到方法参数。
您可能也对这些相关教程感兴趣: [Spring `@GetMapping`教程](/spring/getmapping/), [Spring `DefaultServlet`教程](/spring/defaultservlet/), [Spring Web 应用简介](/articles/springwebfirst/)和 [Java 教程](/lang/java/)。