Using Java 16 records with Thymeleaf

Posted at — Jul 25, 2021
Taming Thymeleaf cover
Interested in learning more about Thymeleaf? Check out my book Taming Thymeleaf. The book combines all of my Thymeleaf knowledge into an easy to follow step-by-step guide.

Since Java 16, we can use records. This blog post shows how Thymeleaf supports this.

For an introduction to Java Records, see Record classes on the Java Language Updates page. For Thymeleaf, there is not that much to explain here as it supports records out-of-the-box. Let’s create a small example application to see things in action.

Start by creating a new Spring Boot application at https://start.spring.io.

Be sure to select:

  • Java 16

  • Dependencies: Web MVC & Thymeleaf

We can create a simple Java record:

public record Person(String givenName, String familyName) {}

Next to that, we create a controller to serve our HTML page:

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

@Controller
public class TestController {

    @GetMapping
    public String index(Model model) {
        model.addAttribute("person", new Person("Wout", "Van Aert"));
        return "index";
    }
}

Note how we add an instance of the Person record to the model.

Finally, create an index.html in the src/main/resources/templates folder:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf with Records</title>
</head>
<body>
<div>
    <h1>Property access</h1>
    <div th:text="${person.givenName}"></div>
    <div th:text="${person.familyName}"></div>

    <h1>Method calls</h1>
    <div th:text="${person.givenName()}"></div>
    <div th:text="${person.familyName()}"></div>
</div>
</body>
</html>

The HTML uses 2 ways of accessing the field values of the record. We can use property access, like we can do for JavaBeans/POJO’s (classes that use the get/set conventions):

<div th:text="${person.givenName}"></div>

We can also call the actual methods that a record creates for each field of the record (without the get/set prefix as records don’t use that):

<div th:text="${person.givenName()}"></div>

If we run the Spring Boot application, we can view our page at http://localhost:8080.

It should look something like this:

thymeleaf with records

Conclusion

Thymeleaf supports records, so if you are on Java 16 or higher, feel free to use them on your Thymeleaf projects.

To see the full code of this example, see GitHub.

If you want to be notified in the future about new articles, as well as other interesting things I'm working on, join my mailing list!
I send emails quite infrequently, and will never share your email address with anyone else.