Skip to content

Thymeleaf

  • Allows including dynamic content in HTML code by means of Thymeleaf expressions
  • Thymeleaf is processed on the server
  • Thymeleaf vs. JSP
  • JSP can only be used in web environment
  • Thymeleaf can be used in web or non-web environments (e.g., email, csv, pdf)
  • Additional Features
  • Looping and conditionals
  • CSS and JS integration
  • Template layouts and fragments

Static folder

  • Spring monitor the following directories for static files
  • src/main/resources/META-INF/resources
  • src/main/resources/resources
  • src/main/resources/static (most common)
  • src/main/resources/public
  • Reference to context path is done with @
<head>
  <title>Thymeleaf Demo</title>
  <link rel="stylesheet" th:href="@{/css/demo.css}" />
</head>

Templates

  • In Spring boot, thymeleaf templates go to src/main/resources/templates
  • Thymeleaf Expressions
  • th:action: location to send form data. th:action="@{/pages/save}"
  • th:object: reference to model attribute. th:object="${employee}"
  • th:field: bind input field to a property on model attribute. th:field="*{firstName}"
  • th:href: reference to a resource. th:href="@{/css/demo.css}"