Practical coding guides

Spring Boot Form Data Binding and Validation Example with FreeMarker

This tutorial will walk you through the steps of creating a Form Data Binding and Validation Example with Spring Boot and FreeMarker

What you’ll build

Spring Boot Form Binding and Validation

Spring Boot Form Binding and Validation

What you’ll need

  • JDK 1.7+
  • Maven 3+


  • Java
  • Spring Boot
  • FreeMarker

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── springboot
│       │               ├── FormController.java
│       │               ├── User.java
│       │               └── WebApplication.java
│       └── resources
│           ├── static
│           │   ├── css
│           │   │   └── main.css
│           │   └── js
│           │       └── main.js
│           ├── templates
│           │   └── form.ftl
│           └── application.properties
└── pom.xml

Project dependencies


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



Define DTO, Controller and View Template

Data transfer object (DTO)


package com.hellokoding.springboot;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

public class User {
    private String firstName;

    private String lastName;

    private String email;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;



package com.hellokoding.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;

public class FormController {
    public String index() {
        return "redirect:/form";

    public String formGet(Model model) {
        model.addAttribute("user", new User());
        return "form";

    public String formPost(@Valid User user, BindingResult bindingResult, Model model) {
        if (!bindingResult.hasErrors()) {
            model.addAttribute("noErrors", true);
        model.addAttribute("user", user);
        return "form";

View template


<#import "/spring.ftl" as spring />

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8">
        <title>Form Data Binding and Validation</title>
        <link href="/css/main.css" rel="stylesheet">
        <h2>Form Data Binding and Validation</h2>

        <@spring.bind "user"/>
        <#if user?? && noErrors??>
            Your submitted data<br>
            First name: ${user.firstName}<br>
            Last name: ${user.lastName}<br>
            Email: ${user.email}<br>
            <form action="/form" method="post">
                First name:<br>
                <@spring.formInput "user.firstName"/>
                <@spring.showErrors "<br>"/>
                Last name:<br>
                <@spring.formInput "user.lastName"/>
                <@spring.showErrors "<br>"/>
                <@spring.formInput "user.email"/>
                <@spring.showErrors "<br>"/>
                <input type="submit" value="Submit">

        <script src="/js/main.js"></script>

Config and Run

Application Configuration


package com.hellokoding.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class WebApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);


spring.freemarker.template-loader-path: classpath:/templates
spring.freemarker.suffix: .ftl

Run the application

Type the following Maven command on your terminal console at the project root directory

mvn clean spring-boot:run

Source code


Follow HelloKoding