<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 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 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>matrixvariableex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring-version>5.1.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.14.v20181114</version> </plugin> </plugins> </build> </project> ``` 在`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<String, String> 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<String, String> 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 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home page</title> </head> <body> <p> <a href="http://localhost:8080/user/first=John/last=Doe">Greet user</a> </p> <p> <a href="http://localhost:8080/data/id=1;first=John;last=Doe;email=johndoe@gmail.com">Show user data</a> </p> <p> <a href="http://localhost:8080/geo/Europe;country=Slovakia;capital=Bratislava">Show country info</a> </p> </body> </html> ``` 這是主頁。 我們有三個鏈接,這些鏈接包含使用`@MatrixVariable`注解解析的名稱/值對。 在本教程中,我們使用`@MatrixVariable`解析路徑段上的名稱/值對并將其綁定到方法參數。 您可能也對這些相關教程感興趣: [Spring `@GetMapping`教程](/spring/getmapping/), [Spring `DefaultServlet`教程](/spring/defaultservlet/), [Spring Web 應用簡介](/articles/springwebfirst/)和 [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看