<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Boot `@RequestParam`教程 > 原文: [http://zetcode.com/springboot/requestparam/](http://zetcode.com/springboot/requestparam/) 在本教程中,我們將在控制器中使用`@RequestParam`注解來讀取請求參數。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## Spring `@RequestParam` `@RequestParam`是一個 Spring 注解,用于將 Web 請求參數綁定到方法參數。 它具有以下可選元素: * `defaultValue` - 當沒有提供請求參數或值為空時用作備用 * `name` - 要綁定到的請求參數的名稱 * `required` - 告訴參數是否為必需 * `value` - 名稱的別名 ## Spring `@RequestParam`示例 以下示例創建一個使用`@RequestParam`的 Spring Boot Web 應用。 我們有一個帶有兩個標簽的 HTML 表單:文本輸入和復選框。 這兩個標記創建請求參數,該參數在控制器中通過`@RequestParam`讀取。 ```java $ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ └── controller │ │ └── MyController.java │ └── resources │ └── static │ └── index.html └── test └── java ``` 這是 Spring Boot 應用的項目結構。 `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>requestparamex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 在 Maven 構建文件中,我們有`spring-boot-starter-web`,它是使用 Spring MVC 構建 Web 應用的入門。 它使用 Tomcat 作為默認的嵌入式容器。 `spring-boot-devtools`是在開發 Spring Boot 應用時有用的構件。 它允許自動重啟或實時重新加載應用。 該應用打包到一個 JAR 文件中。 `com/zetcode/MyController.java` ```java package com.zetcode.controller; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE) @ResponseBody public String processForm(@RequestParam(defaultValue="Guest") String name, @RequestParam(required = false) String adult) { var greet = "on".equals(adult) ? "Good morning" : "Hi"; var message = String.format("%s %s!", greet, name); return message; } } ``` 控制器處理 HTML 表單。 它從請求中讀取兩個參數。 ```java @Controller public class MyController { ``` 在 Spring 中,控制器類使用`@Controller`注解進行注解。 ```java @RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE) @ResponseBody ``` `processForm()`方法映射到`/message`路徑并返回純文本。 `@ResponseBody`注解指示方法返回值已綁定到 Web 響應正文。 ```java public String processForm(@RequestParam(defaultValue="Guest") String name, @RequestParam(required = false) String adult) { ``` 使用`@RequestParam`注解,我們將請求參數綁定到方法變量。 如果參數不可用(文本輸入為空),則`defaultValue`選項將提供默認值。 `required`選項表明該參數是必需的。 該方法重新調整字符串。 ```java var greet = "on".equals(adult) ? "Good morning" : "Hi"; var message = String.format("%s %s!", greet, name); return message; ``` 我們構建消息并返回。 `resources/static/index.html` ```java <!DOCTYPE html> <html lang="en"> <head> <title>Home page</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </head> <body> <form action="message"> <div> <label>Name:</label> <input type="text" name="name"> </div> <div> <label><input type="checkbox" name="adult">Adult</label> </div> <button type="submit">Submit</button> </form> </body> </html> ``` `index.html`文件是主頁。 該文件位于`src/main/resources/static`目錄中,Spring Boot 在該目錄中需要靜態資源,例如 HTML 或 CSS 文件。 我們有一個帶有輸入文本和復選框標簽的簡單 HTML 表單。 ```java <form action="message"> ``` `action`選項包含在控制器方法映射中使用的字符串。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`是設置 Spring Boot 應用的入口。 `@SpringBootApplication`注解啟用自動配置和組件掃描。 ```java $ mvn spring-boot:run ``` 應用運行后,我們可以導航到`localhost:8080`。 在本教程中,我們使用 Spring Boot 框架創建了 Web 應用。 我們已經演示了`@RequestParam`的用法。 您可能也對相關教程感興趣: [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#springboot)。
                  <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>

                              哎呀哎呀视频在线观看