Hidden Inputs in Thymeleaf

November 02, 2020 No comments Thymeleaf input hidden

1. Introduction

Thymeleaf is a Java-based template engine used to process documents like HTML, XML, and others. It is one of the most popular libraries used to generate dynamic websites in Java applications. In this article we are going to present how to use hidden inputs in Thymeleaf templates.

2. Thymeleaf forms

Let's start with creating a simple form in Thymeleaf and break it down into its components.

<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8"/>
    <title>Spring Boot Thymeleaf Application - Forms</title>

<form th:action="@{/posts}" th:object="${post}" method="post">
        <label for="title">Title:</label>
        <input id="title" type="text" th:field="*{title}" placeholder="Title"/>
        <label for="content">Content:</label>
        <textarea id="content" rows="2" placeholder="Content" th:field="*{content}"></textarea>

    <input type="hidden" th:field="*{id}"/>
    <input type="submit" value="Submit"/>


There are several important aspects we need to explain here:

  • every form in Thymeleaf used a simple HTML form tag,
  • form could be associated with Java object provided in th:object attribute - this object is called command object,
  • command object properties can be attached to inputs using th:field attribute,
  • action could be specified using Thymeleaf attribute th:action or simply action.

Hidden fields are similar to regular one the different is in type. For hidden fields we used type="hidden":

<input type="hidden" th:field="*{id}"/>

Java object associated with this form has the following structure:

package com.frontbackend.thymeleaf.bootstrap.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class Post {

    private String id;
    private String title;
    private String content;

Now let's have a look at a simple controller that could handle GET and POST requests:

package com.frontbackend.thymeleaf.bootstrap.controller;

import java.util.UUID;

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

import com.frontbackend.thymeleaf.bootstrap.model.Post;

@RequestMapping({ "/", "/posts" })
public class PostController {

    public String main(Model model) {
        Post post = new Post();
        model.addAttribute("post", post);
        return "index";

    public String save(Post post, Model model) {
        model.addAttribute("post", post);

        // call service to save the post

        return "saved";

Every GET request to the root context / or /posts path will present a form associated with a brand new Post object filled with a unique id. POST requests handled by the save method are intended to save post data submitted in HTML form.

Using th:field is one of the way to assign a value to a hidden input, but we could use also:

  • simple name attribute:

    <input type="hidden" th:value="${blog.id}" name="id" />
  • or th:attr Thymeleaf attribute:

    <input type="hidden" th:value="${blog.id}" th:attr="name='id'"/>

3. Conclusion

In this article, we presented how to create a form with hidden fields in Thymeleaf. We showed several ways to achieve that in a real-life but simple example.

As usual, all the code examples used in this tutorial can be found over on Github.

{{ message }}

{{ 'Comments are closed.' | trans }}