What is Thymeleaf and why is it needed?
\n\nThymeleaf is a modern server-side template engine for Java applications, designed for generating HTML, XML, JavaScript, CSS, and text files. Unlike JSP (JavaServer Pages), Thymeleaf offers a cleaner, more readable, and natural syntax that is easy for both developers and designers to understand.
\n\nThe main advantage of Thymeleaf is that its templates are valid HTML documents. This means you can open a template in a browser without running a server and see its structure. Dynamic data is inserted using special attributes starting with the th: prefix.
Thymeleaf integrates perfectly with the Spring Framework (especially Spring Boot), but can also be used as a standalone library. It supports layouts, fragments, conditional constructs, loops, form handling, internationalization (i18n), and much more.
\n\nInstalling Thymeleaf
\n\nSince Thymeleaf is a Java library, it is installed via dependency management systems like Maven or Gradle. There is no equivalent of pip install for Java.
Installation via Maven (pom.xml)
\n\n<dependency>\n <groupId>org.thymeleaf</groupId>\n <artifactId>thymeleaf</artifactId>\n <version>3.1.2.RELEASE</version>\n</dependency>\n\nInstallation via Gradle (build.gradle)
\n\nimplementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'\n\nFor Spring Boot (recommended)
\n\nIf you are using Spring Boot, simply add the starter:
\n\n<dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-thymeleaf</artifactId>\n</dependency>\n\nSpring Boot will automatically configure Thymeleaf with sensible default settings.
\n\nQuick Start with Code Example
\n\nLet's create a simple console application that uses Thymeleaf to generate HTML.
\n\nStep 1: Create a template (src/main/resources/templates/hello.html)
\n\n<!DOCTYPE html>\n<html xmlns:th="http://www.thymeleaf.org">\n<head>\n <title>Hello Thymeleaf</title>\n</head>\n<body>\n <h1 th:text="${message}">Default Message</h1>\n <p>Current date: <span th:text="${#dates.format(#dates.createNow(), 'dd-MM-yyyy')}">01-01-2024</span></p>\n</body>\n</html>\n\nStep 2: Write Java code to process the template
\n\nimport org.thymeleaf.TemplateEngine;\nimport org.thymeleaf.context.Context;\nimport org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;\n\npublic class ThymeleafQuickStart {\n public static void main(String[] args) {\n // 1. Configure the template resolver\n ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();\n resolver.setPrefix("/templates/");\n resolver.setSuffix(".html");\n resolver.setTemplateMode("HTML");\n\n // 2. Create the template engine\n TemplateEngine engine = new TemplateEngine();\n engine.setTemplateResolver(resolver);\n\n // 3. Create a context with data\n Context context = new Context();\n context.setVariable("message", "Hello, Thymeleaf!");\n\n // 4. Process the template\n String result = engine.process("hello", context);\n \n System.out.println(result);\n }\n}\n\nExecution result:
\n\n<!DOCTYPE html>\n<html>\n<head>\n <title>Hello Thymeleaf</title>\n</head>\n<body>\n <h1>Hello, Thymeleaf!</h1>\n <p>Current date: <span>15-03-2024</span></p>\n</body>\n</html>