Thymeleaf Message Expression

In this tutorial, we will learn how to use Thymeleaf Message Expression in a Thymeleaf HTML template with an example.

Check out the complete Thymeleaf tutorials and examples at Thymeleaf Tutorial

Message expressions let you externalize common texts into a properties file. 

Let’s say you have a welcome message that you want to show on every view. However, hardcoding this message on all of these views is a bad idea.

Message Expressions​ Example

Let's create a Spring boot project using the spring initializr and add Spring Web and Thymeleaf dependencies:

User Model Class

Next, let's create a User model class with the following content into it:
package net.javaguides.thymeleaf.model;

public class User {
    private String name;
    private String email;
    private String role;
    private String gender;

    public User(String name, String email, String role, String gender) { = name; = email;
        this.role = role;
        this.gender = gender;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String getRole() {
        return role;

    public void setRole(String role) {
        this.role = role;

    public String getGender() {
        return gender;

    public void setGender(String gender) {
        this.gender = gender;

Create File

Next, let's create a file under /resources folder and add the following content: Boot Thymeleaf Application
welcome.message=Hello, welcome to Spring boot application

Spring MVC Controller - UserController

Let's add the below handler method in a UserController to return the Thymeleaf template like:
package net.javaguides.thymeleaf.controller;

import net.javaguides.thymeleaf.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class UserController {

    // handler method to handle message expressions request
    // http://localhost:8080/message-expression
    public String messageExpression(){
        return "message-expression";
Spring boot will auto-configure ViewResolver for Thymeleaf whenever it will find the springboot-thymeleaf-starter dependency on the classpath hence we don't have to manually configure ViewResolver for Thymeleaf.

Thymeleaf Template - message-expression.html

Here is the Thymeleaf template that demonstrates the usage of Message expressions:
<!DOCTYPE html>
<html lang="en"
    <meta charset="UTF-8">
    <title>Message Expressions</title>
<h1>Message Expressions Demo:</h1>
<h2 th:text="#{}"></h2>
<h2 th:text="#{welcome.message}"></h2>


Run the Spring boot application and hit the below link in the browser:

Here is the output:

Related Thymeleaf Standard Expressions

  1. Thymeleaf Standard Expressions
  2. Thymeleaf th:text Attribute
  3. Thymeleaf Variable Expression
  4. Thymeleaf Selection Expression
  5. Thymeleaf Message Expression
  6. Thymeleaf Link Expression
  7. Thymeleaf Fragment Expression