SpringBoot 提交表单教程展示了如何在 Spring Boot 应用中提交表单。
Spring 是流行的 Java 应用框架。 Spring Boot 致力于以最小的努力创建独立的,基于生产级别的基于 Spring 的应用。
Spring Boot 提交表单示例
以下应用包含一个简单的表格。 来自表单的数据会自动插入到 UI Bean 中,并且可用于视图。 Thymeleaf 用作视图引擎。
pom.xmlsrc├───main│ ├───java│ │ └───com│ │ └───zetcode│ │ │ Application.java│ │ ├───bean│ │ │ User.java│ │ └───controller│ │ MyController.java│ └───resources│ └───templates│ addUser.html│ showMessage.html└───test└───java
这是项目结构。
pom.xml
<?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.0http://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
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
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;@Controllerpublic class MyController {@GetMapping("/addUser")public String sendForm(User user) {return "addUser";}@PostMapping("/addUser")public String processForm(User user) {return "showMessage";}}
控制器类发送和读取表单视图。
@PostMapping("/addUser")public String processForm(User user) {return "showMessage";}
User bean 作为参数传递给processForm()处理器。 Spring 尝试用请求数据填充 Bean。 数据也可自动用于 Thymeleaf showMessage视图。
com/zetcode/Application.java
package com.zetcode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
Application设置 Spring Boot 应用
resources/templates/addUser.html
<!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>
该视图包含表单。
<form action="#" th:action="@{/addUser}" th:object="${user}" method="post">
th:object引用user表单 bean。 这不是一个类名,而是一个 Spring bean 名称。 因此它是小写的。
<p>Name: <input type="text" th:field="*{name}"></p>
使用*{}语法,我们引用已定义的对象。
resources/templates/showMessage.html
<!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>
该模板显示在表单中输入的数据。
<p th:text="'Name: ' + ${user.name}"></p>
我们使用${}语法引用表单 bean 属性。
导航至localhost:8080/addUser以测试应用。
在本教程中,我们展示了如何在 Spring Boot 应用中提交简单表单。 您可能也对相关教程感兴趣: Spring Boot 第一个 Web 应用, Spring Boot RESTFul 应用, Spring Boot @Controller教程,独立 Spring 应用和 Java 教程。
