Thymeleaf क्या है और इसकी आवश्यकता क्यों है?
Thymeleaf एक आधुनिक सर्वर-साइड टेम्पलेट इंजन है जो Java एप्लिकेशन के लिए HTML, XML, JavaScript, CSS और टेक्स्ट फ़ाइलें जनरेट करता है। JSP (JavaServer Pages) के विपरीत, Thymeleaf का सिंटैक्स अधिक साफ, पढ़ने में आसान और प्राकृतिक है, जिसे डेवलपर और डिज़ाइनर दोनों आसानी से समझ सकते हैं।
Thymeleaf की सबसे बड़ी खासियत यह है कि इसके टेम्पलेट वैध HTML दस्तावेज़ होते हैं। इसका मतलब है कि आप सर्वर चलाए बिना ब्राउज़र में टेम्पलेट खोल सकते हैं और उसकी संरचना देख सकते हैं। डायनामिक डेटा को th: प्रीफ़िक्स से शुरू होने वाले विशेष एट्रिब्यूट के ज़रिए डाला जाता है।
Thymeleaf Spring Framework (खासकर Spring Boot) के साथ पूरी तरह से इंटीग्रेट होता है, लेकिन इसे एक स्वतंत्र लाइब्रेरी के रूप में भी इस्तेमाल किया जा सकता है। यह लेआउट, फ़्रैगमेंट, कंडीशनल स्टेटमेंट, लूप, फ़ॉर्म हैंडलिंग, इंटरनेशनलाइज़ेशन (i18n) और बहुत कुछ सपोर्ट करता है।
Thymeleaf की स्थापना (Installation)
चूंकि Thymeleaf एक Java लाइब्रेरी है, इसे Maven या Gradle जैसी डिपेंडेंसी मैनेजमेंट सिस्टम के ज़रिए इंस्टॉल किया जाता है। Java के लिए pip install जैसा कोई विकल्प नहीं है।
Maven (pom.xml) के ज़रिए स्थापना
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
Gradle (build.gradle) के ज़रिए स्थापना
implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'
Spring Boot के लिए (अनुशंसित)
अगर आप Spring Boot का उपयोग कर रहे हैं, तो बस स्टार्टर जोड़ें:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot अपने आप Thymeleaf को उचित डिफ़ॉल्ट सेटिंग्स के साथ कॉन्फ़िगर कर देगा।
कोड उदाहरण के साथ त्वरित शुरुआत
चलिए एक सरल कंसोल एप्लिकेशन बनाते हैं जो HTML जनरेट करने के लिए Thymeleaf का उपयोग करता है।
चरण 1: टेम्पलेट बनाएं (src/main/resources/templates/hello.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello Thymeleaf</title>
</head>
<body>
<h1 th:text="${message}">Default Message</h1>
<p>आज की तारीख: <span th:text="${#dates.format(#dates.createNow(), 'dd-MM-yyyy')}">01-01-2024</span></p>
</body>
</html>
चरण 2: टेम्पलेट प्रोसेस करने के लिए Java कोड लिखें
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
public class ThymeleafQuickStart {
public static void main(String[] args) {
// 1. टेम्पलेट रिज़ॉल्वर सेट करें
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML");
// 2. टेम्पलेट इंजन बनाएं
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(resolver);
// 3. डेटा के साथ कॉन्टेक्स्ट बनाएं
Context context = new Context();
context.setVariable("message", "नमस्ते, Thymeleaf!");
// 4. टेम्पलेट प्रोसेस करें
String result = engine.process("hello", context);
System.out.println(result);
}
}
आउटपुट:
<!DOCTYPE html>
<html>
<head>
<title>Hello Thymeleaf</title>
</head>
<body>
<h1>नमस्ते, Thymeleaf!</h1>
<p>आज की तारीख: <span>15-03-2024</span></p>
</body>
</html>