Thymeleaf Commands

Published: 2019-08-04, Updated: 2021-01-19


Chamar método estático

<span th:text="${@thymeleaf.ThymeleafUtils@analyticsId()}"></span>

Incluir scripts

<script th:utext="'' + *{report}"></script>
<script th:src="@{/assets/js/gerencial-relatorio.js}"></script>

Declarar variável

<div th:with="someVariable=${someValue}">

Imprimir variavel concatenada ao inves de substituir tudo

<h1 th:inline="text" >
	 <small th:text="${header.subtitle}">Subtitle</small>

Incluir fragmentos nas paginas


<!DOCTYPE html>
<html xmlns="" xmlns:th="">
<div th:fragment="results">
some content


<!DOCTYPE html>
<html xmlns="" xmlns:th=""
<body >
	<div layout:fragment="content" >
		<div th:replace="fragment/results :: results"></div>

Montando layout / Template


Forma recomendada

Forma 1 (Só funciona com Spring)

Forma 2

with Thymeleaf 2.1, you can write something like that:

Create the template (for ex. templates/layout.html), and add the th:fragment="page" information in html tag and define the content area with th:include="this :: content" information:

<!DOCTYPE html>
<html xmlns="" xmlns:th="" th:fragment="page">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		layout page
		<div th:include="this :: content"/>
		layout footer

Now create the page that will include this template adding th:include="templates/layout :: page" in html tag and put your main content inside a div with th:fragment="content"

<!DOCTYPE html>
<html xmlns=""
	th:include="templates/layout :: page">
		<div th:fragment="content">
			my page content

In layout page you can use this (th:include="this :: content") or suppress this option (th:include=":: content"). It seems like jsf facelets I think.

Concatenar strings

th:text="#{App.Title} + #{user.list}"

If not thymeleaf

<div th:if="${geometry} == null" >
	estah nulo

Usando as expressions

Dar esse se for nulo


Nao dar erro se for nulo



<span th:text="${ == null} ? 'Não Informado' : ${}" />

Montando um link com query params

<td th:with="oaeClassificationId=${c.oaeClassificationId == null} ? '' :  ${c.oaeClassificationId},classifyLink='?oaeId=' + ${c.oaeId} + '&amp;classificationId=' + ${oaeClassificationId}">
<a th:href="@{/classificacao-oae.html} + ${classifyLink}"
	 th:if="${c.oaeClassificationId != null}"
	 class="btn btn-primary waves-effect waves-light margin-bottom-5"

Usando ModelAttribute para mapear um form dentro de uma propriedade

public class FormTestController {

	@RequestMapping(value = "/form-test-1.jhtml", method = RequestMethod.GET)
	public String formTest1(@ModelAttribute("form1") Form1TestVO form1TestVO, Model model){
		model.addAttribute("form1", new Form1TestVO("Form 1 test"));
		return "form-test-1";


<!DOCTYPE html>
<html xmlns="" xmlns:th="" xmlns:layout="" >
	<title>Form test 1</title>
<body >

	<form th:object="${form1}" th:action="@{/form-test-1.jhtml}" >
		<input  th:field="*{name}" />



public class Form1TestVO {
	private String name;

	public Form1TestVO() {

	public Form1TestVO(String name) { = name;

	public String getName() {
		return name;

	public void setName(String name) { = name;

Setar o nome e valor do campo via thymeleaf

<td class="text-center"><input value="" required="" th:field="*{durableClassificationPoint.mesosStructure}" type="number"  min="1" max="5"/></td>

Imprimir texto sem espacar por exemplo JSON

<script th:utext="${json}"></script>

Deixar um select com valor default marcado

<select id="executiveProcedureId" th:field="*{executiveProcedureId}" required="" class="form-control">
	<option value=""></option>
	<option th:each="ep : ${procedures}" th:value="${}" th:text="${ep.description}"></option>

Chamar classe / metodo java

package com.mageddo.professionals.utils;

import java.time.LocalDate;

public class DateUtils {
	public static int currentYear(){
<span class="tm-current-year" th:text="${T(com.mageddo.professionals.utils.DateUtils).currentYear()}"></span>


Por default thymeleaf escapa HTML, JSON, etc, para forcar que ele nao esape faça assim

<span th:utext="${someVar}" />

Exemplos de iteração

<tr th:each="c : ${classfifications}">
	<td th:text="*{c.oaeId}"></td>
	<td th:text="*{c.oaeName}"></td>
		<span class="label label-info">Ativo</span>
		<a th:href="@{/gerencial-relatorio.html} + '?oaeId=' + *{c.oaeId}" class="btn btn-primary waves-effect waves-light margin-bottom-5">Gerar Relatório</a>

Custom parameter / custom attribute

<a th:attr="data-post-id=${}" />

Laravel Commands Atalhos de teclado do Youtube