English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Thymeleaf is an open-source Java library based on Apache license2.0 to obtain a license. This is a HTML5/XHTML/XML template engine. It is used for web (based on Servlet) and non-web (offline) environments server-side Java templateengine. For modern HTML5 JVM Web development, it is a perfect choice. It provides full integration with the Spring Framework.
It applies a set of transformations to the template file to display the data or text generated by the application. It is suitable for providing XHTML in web applications./HTML5.
The goal of Thymeleaf is to provide a fashionableand well-formedtemplate creation method. It is based on XML tags and attributes. These XML tags define the execution of predefined logic on the DOM (Document Object Model), rather than explicitly writing the logic as code within the template. It replaces JSP .
The architecture of Thymeleaf allows for the fast processing, depending on the cached parsed files. It uses the least I/O operation.
JSP is more or less similar to HTML. However, it is not fully compatible with HTML like Thymeleaf. We can normally open and display Thymeleaf template files in the browser, but JSP files cannot.
Thymeleaf supports variable expressions like Spring EL ($ {...}) and executes them on model attributes and asterisk expressions (* {...}) are executed on the form support bean, hash expressions (#{...}) are used for internationalization, and link expressions (@{......)) rewrite URLs.
Like JSP, Thymeleaf can be used very well for Rich HTML emails.
Thymeleaf can handle six types of templates (also known as template pattern) as follows:
XML valid XML XHTML valid XHTML HTML5 old version of HTML5
except for the old version of HTML5beyond the pattern, all the above patterns are referenced well-defined XML file. It allows us to handle HTML with independent tags, tag attributes without values, or text not written within quotes, and other functions5file.
To process files in this specific pattern, Thymeleaf will perform a transformation, converting files into well-formed XML File (valid HTML5file).
Thymeleaf also allows us to define our own patterns by specifying two ways to parse templates in this mode. In this way, Thymeleaf can effectively process any model that can be modeled as a DOM tree as a template.
Thymeleaf is a template engine that allows us to define the framework for DOM nodes. The DOM nodes processed in the template.
The object that applies logic to DOM nodes is called processors. A set of processors and some additional artifacts are called dialect. A dialect that includes the Thymeleaf core library is called Standard dialect.
If we want to define our own processing logic while taking advantage of the advanced features of the library, we can define our own dialect. We can define our own dialect. In the template engine, we can configure multiple dialects at once.
Thymeleaf integration package (thymeleaf-spring3and thymeleaf-spring4) defines a called SpringStandard Dialect dialect. The standard dialect is almost the same as SpringStandard. However, the standard dialect has some minor changes that can better utilize certain features of the Spring framework.
For example, using Spring Expression Language instead of Thymeleaf's standard OGNL (Object Graph Navigation Language).
The standard dialect can handle templates in any way. However, it is very suitable for web-oriented template patterns (HTML5and XHTML). It supports and validates the following XHTML specifications:
XHTML 1.0 transitional version strict XHTML 1.0 XHTML 1.0 framework set XHTML 1.1.
Standard dialect processor is a property processor that allows the browser to display HTML5/XHTML template file. This is because they ignore other attributes.
For example, when a JSP file uses a tag library, it contains some code that cannot be displayed by the following browser:
<form:inputText name="student.Name" value="${student.name}" />
Thymeleaf standard dialect allows us to achieve the same functionality with the following code.
<input type="text" name="student Name" value="Thomas" th:value="${student.name}" />
The above code also allows us to define value attribute ( Thomas When opening the prototype in the browser, this value will be displayed. During the Thymeleaf template processing, this property will be handled by the ${student.name} The value obtained from the calculation replaces it.
It allows designers and developers to handle the same template files, reducing the work required to convert static prototypes into working template files. It is called Natural template.
It can be used in both network and non-network environments. for HTML5/XML/XHTML Java template engine. Its high-performance parsing template cache will I/O to the lowest. It can be used as a template engine framework if needed. It supports several template modes: XML, XHTML, and HTML5. It allows developers to extend and create custom dialects. It is based on a modular feature set called dialects. It supports internationalization.
<dependency> <groupId>org.springframework.boot</<groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
We create a Spring Boot application and implement Thymeleaf templates.
Steps1: Open Spring Initializr http://start.spring.io .
Steps2: Select the Spring Boot version 2.3.0.M1.
Steps2: provide GroupName. We provide com.w3codebox .
Steps3: provide Artifact ID. We provide spring-boot-thymeleaf-view-example.
Steps5: Add dependencies Spring Web and Thymeleaf.
Steps6: click Generate (Generate) button. When we click the "Generate" button, it will package the specifications in Jar file and download it to the local system.
the7Step: Extract Jar file and paste it into the STS workspace.
Steps8: Import STS project folder.
File->Import->Existing Maven project->Browse->Select folder spring-boot-thymeleaf-view-example->Done
Importing is taking some time.
Steps9: in the package com.w3codebox In the package, we create a class named User class.
In this class, we define two variables name and email and generate Getter and Setters.
User.java
package com.w3codebox; public class User { String name; String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Steps10: Create a controller class. We created a class named DemoController is the controller class.
DemoController.java
package com.w3codebox; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.stereotype.Controller; @Controller public class DemoController { @RequestMapping("/") public String index() { return"index"; } @RequestMapping(value="/save", method=RequestMethod.POST) public ModelAndView save(@ModelAttribute User user) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("user-data"); modelAndView.addObject("user", user); return modelAndView; } }
Next, we will create a Thymeleaf template.
Steps11: at inside the template(src/main/resources/folder, create a file named user-data Thymeleaf template.
Right-click on the template folder->New->Other->HTML file->Next->Provide a file name->Done
<html lang="en" xmlns:th="http://www.thymeleaf.org">
user-data.html
<html xmlns:th="https://thymeleaf.org"> <table> <tr> <td><h4>User Name: </h4></td> <td><h4 th:text="${user.name}"></h4></td> </tr> <tr> <td><h4>Email ID: </h4></td> <td><h4 th:text="${user.email}"></h4></td> </tr> </table> </html>
Steps12: Similarly, create a file in the template folder. HTML file. We created a file named index HTML file.
index.html
<html lang="en"> <head> <title>Index Page</title> </head> <body> <form action="save" method="post"> <table> <tr> <td><label for="user-name">User Name</label></td> <td><input type="text" name="name"></input></td> </tr> <tr> <td><label for="email">Email</label></td> <td><input type="text" name="email"></input></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></input></td> </tr> </table> </form> </body> </html>
Steps13: Open application.properties file and add the following properties to it.
application.properties
spring.thymeleaf.cache=false spring.thymeleaf.suffix: .html
After creating all files, folders, and packages, the project directory is as follows:
Let's run the application.
Steps14: Open SpringBootThymeleafViewExampleApplication.java file as a Java application.
SpringBootThymeleafViewExampleApplication.java
package com.w3codebox; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootThymeleafViewExampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootThymeleafViewExampleApplication.class, args); } }
Steps15: Now, open the browser and call the URL http://localhost:8080. It displays the output as shown below.
provide usernameand emailthen click submitthe button.
click submitbutton, change the URL to http://localhost: 8080/save and display the user data as shown below.
In this section, we discuss the Thymeleaf views. To make the view more attractive, you can add CSS and JS The files must be located in src/main/resources/static in the folder.