Thymeleaf is a modern server-side java template engine commonly used to build web applications. It is popular in Spring Boot projects for server side rendering of dynamic HTML pages. It has natural templating feature meaning HTML file can be directly opened in browser even without being processed by server. Below is a basic Thymeleaf program that showcases how Thymeleaf can be used with minimal configurations in Eclipse IDE.
1. At first Eclipse, STS or any IDE of preference is to be opened.
Launch IDE with a workspace of choice
2. Next it requires creating a new Spring Project by following below step File > New > Spring Starter Project
Next it requires to name the project and selecting Maven as a build tool then pressing next
Next it requires searching and checking the Spring Web dependency
Next Thymeleaf dependency is to be checked and then finish button is to be selected
3. Next some configurations will be loaded and the final project structure can be seen as in below image
4. Next it requires navigating to the templates directory for creating the HTML files and templates is the default location for placing the Thymeleaf HTML file templates
Next it requires naming the Thymeleaf HTML file and next hitting the Finish button
5. Next insert the Thymeleaf boilerplate HTML code as provided below
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="ISO-8859-1">
<title>Thymeleaf Test</title>
</head>
<body>
<h2>Thymeleaf Application Test Success</h2>
</body>
</html>
6. Next it requires locating the main file for defining route and running the Thymeleaf application
7. Next define the /thymeleaf-test route in the main function as provided below
Leave the first line of code intact for the package declaration and replace the file with below provided code or manually edit the file with code provided below:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
@SpringBootApplication
public class ThymeleafTestApplication {
@GetMapping("/thymeleaf-test")
public String testingThymeleaf() {
return "thymeleaf_test";
}
public static void main(String[] args) {
SpringApplication.run(ThymeleafTestApplication.class, args);
}
}
Next after successful compilation the final output can be seen in the browser by navigating the localhost:8080/thymeleaf-test route in the browser















No comments:
Post a Comment