Ebooks

Spring Boot submit form tutorial

SpringBoot submit form tutorial shows how to submit a form in a Spring Boot application.

Spring is a popular Java application framework. Spring Boot is an effort to create stand-alone, production-grade Spring based applications with minimal effort.

Spring Boot submit form example

The following application contains a simple form. The data from a form is automatically inserted into a UI bean and is available for a view. Thymeleaf is used as a view engine.

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           ├───bean
│   │           │       User.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       └───templates
│               addUser.html
│               showMessage.html
└───test
    └───java

This is the project structure.

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

This is the Maven build file. The spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC. The spring-boot-starter-thymeleaf is a starter for the Thymeleaf engine. When Spring locates the dependency in the pom.xml, it automcatically configures Thymeleaf for us.

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

This is the User bean. It is automatically filled with data from the form request. The attributes must match the form fields.

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;

@Controller
public class MyController {

    @GetMapping("/addUser")
    public String sendForm(User user) {

        return "addUser";
    }

    @PostMapping("/addUser")
    public String processForm(User user) {

        return "showMessage";
    }
}

The controller class sends and reads a form view.

@PostMapping("/addUser")
public String processForm(User user) {

    return "showMessage";
}

The User bean is passed as a parameter to the processForm() handler. Spring tries to fill the bean with the request data. The data is also automatically available for the Thymeleaf showMessage view.

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;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

The Application sets up the Spring Boot application

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>

This view contains the form.

<form action="#" th:action="@{/addUser}" th:object="${user}" method="post">

The th:object refers to the user form bean. This is not a class name, but a Spring bean name; therefore it is in lowercase.

<p>
    Name: <input type="text" th:field="*{name}">
</p>

With the *{} syntax, we refer to the defined object.

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>

This template shows the data entered in the form.

<p th:text="'Name: ' + ${user.name}"></p>

We refer to the form bean attributes with the ${} syntax.

Navigate to the localhost:8080/addUser to test the application.

In this tutorial, we have shown how to submit a simple form in a Spring Boot application. You might also be interested in the related tutorials: Spring Boot first web application, Spring Boot RESTFul application, Spring Boot @Controller tutorial, Standalone Spring applications, and Java tutorial.