<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 提交表單教程 > 原文: [http://zetcode.com/springboot/submitform/](http://zetcode.com/springboot/submitform/) SpringBoot 提交表單教程展示了如何在 Spring Boot 應用中提交表單。 Spring 是流行的 Java 應用框架。 Spring Boot 致力于以最小的努力創建獨立的,基于生產級別的基于 Spring 的應用。 ## Spring Boot 提交表單示例 以下應用包含一個簡單的表格。 來自表單的數據會自動插入到 UI Bean 中,并且可用于視圖。 Thymeleaf 用作視圖引擎。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───bean │ │ │ User.java │ │ └───controller │ │ MyController.java │ └───resources │ └───templates │ addUser.html │ showMessage.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>SpringBootSubmitFormEx</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.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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(包括 RESTful)應用的入門程序。 `spring-boot-starter-thymeleaf`是 Thymeleaf 發動機的啟動器。 當 Spring 在`pom.xml`中找到依賴項時,它將自動為我們配置 Thymeleaf。 `com/zetcode/bean/User.java` ```java package com.zetcode.bean; public class User { private String name; private String occupation; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOccupation() { return occupation; } public void setOccupation(String occupation) { this.occupation = occupation; } } ``` 這是`User` bean。 它會自動填充表單請求中的數據。 這些屬性必須與表單字段匹配。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.bean.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class MyController { @GetMapping("/addUser") public String sendForm(User user) { return "addUser"; } @PostMapping("/addUser") public String processForm(User user) { return "showMessage"; } } ``` 控制器類發送和讀取表單視圖。 ```java @PostMapping("/addUser") public String processForm(User user) { return "showMessage"; } ``` `User` bean 作為參數傳遞給`processForm()`處理器。 Spring 嘗試用請求數據填充 Bean。 數據也可自動用于 Thymeleaf `showMessage`視圖。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`設置 Spring Boot 應用 `resources/templates/addUser.html` ```java <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Add user</title> <meta charset="UTF-8"> </head> <body> <h1>Add User</h1> <form action="#" th:action="@{/addUser}" th:object="${user}" method="post"> <p> Name: <input type="text" th:field="*{name}"> </p> <p> Occupation: <input type="text" th:field="*{occupation}"> </p> <p> <input type="submit" value="Submit"/> <input type="reset" value="Reset"> </p> </form> </body> </html> ``` 該視圖包含表單。 ```java <form action="#" th:action="@{/addUser}" th:object="${user}" method="post"> ``` `th:object`引用`user`表單 bean。 這不是一個類名,而是一個 Spring bean 名稱。 因此它是小寫的。 ```java <p> Name: <input type="text" th:field="*{name}"> </p> ``` 使用`*{}`語法,我們引用已定義的對象。 `resources/templates/showMessage.html` ```java <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Show message</title> <meta charset="UTF-8"> </head> <body> <h1>Result</h1> <p th:text="'Name: ' + ${user.name}"></p> <p th:text="'Occupation: ' + ${user.occupation}"></p> <a href="/addUser">Submit another message</a> </body> </html> ``` 該模板顯示在表單中輸入的數據。 ```java <p th:text="'Name: ' + ${user.name}"></p> ``` 我們使用`${}`語法引用表單 bean 屬性。 導航至`localhost:8080/addUser`以測試應用。 在本教程中,我們展示了如何在 Spring Boot 應用中提交簡單表單。 您可能也對相關教程感興趣: [Spring Boot 第一個 Web 應用](/articles/springbootwebfirst/), [Spring Boot RESTFul 應用](/articles/springbootrestsimple/), [Spring Boot `@Controller`教程](/springboot/controller/),[獨立 Spring 應用](/articles/standalonespring/)和 [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>

                              哎呀哎呀视频在线观看