Spring Boot ThymeleafWhat is Thymeleaf?The Thymeleaf is an open-source Java library that is licensed under the Apache License 2.0. It is a HTML5/XHTML/XML template engine. It is a server-side Java template engine for both web (servlet-based) and non-web (offline) environments. It is perfect for modern-day HTML5 JVM web development. It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates. It is based on XML tags and attributes. These XML tags define the execution of predefined logic on the DOM (Document Object Model) instead of explicitly writing that logic as code inside the template. It is a substitute for JSP. The architecture of Thymeleaf allows the fast processing of templates that depends on the caching of parsed files. It uses the least possible amount of I/O operations during execution. Why we use Thymeleaf?JSP is more or less similar to HTML. But it is not completely compatible with HTML like Thymeleaf. We can open and display a Thymeleaf template file normally in the browser while the JSP file does not. Thymeleaf supports variable expressions (${...}) like Spring EL and executes on model attributes, asterisk expressions (*{...}) execute on the form backing bean, hash expressions (#{...}) are for internationalization, and link expressions (@{...}) rewrite URLs. Like JSP, Thymeleaf works well for Rich HTML emails. What kind of templates can the Thymeleaf process?Thymeleaf can process six types of templates (also known as Template Mode) are as follows:
Except for the Legacy HTML5 mode, all the above modes refer to well-defined XML files. It allows us to process HTML5 files with features such as standalone tags, tag attributes without value, or not written between quotes. To process files in this specific mode, Thymeleaf performs a transformation that converts files into a well-formed XML file (valid HTML5 file). Note: In Thymeleaf, validation is available only for XHTML and XML template.Thymeleaf also allows us to define our own mode by specifying both a way to parse templates in this mode. In this way, whatever that can be modeled as a DOM tree could effectively be processed as a template by Thymeleaf. The Standard DialectThymeleaf is a template engine framework that allows us to define the DOM nodes. The DOM nodes processed in the templates. An object that applies logic to a DOM node is called processor. A set of processors, along with some extra artifacts, is called the dialect. The dialect that contains the Thymeleaf's core library is called the Standard Dialect. If we want to define our own processing logic while taking advantage of the library's advanced features, we can define our own dialects. In a template engine, we can configure several dialects at a time. The Thymeleaf integration packages (thymeleaf-spring3 and thymeleaf-spring4) define a dialect called SpringStandard Dialect. The Standard Dialect and SpringStandard are almost the same. But the Standard Dialect has some small changes that make better use of some features in Spring Framework. For example, use Spring Expression Language instead of Thymeleaf's standard ONGL (Object-Graph Navigation Language). The Standard Dialect can process template in any mode. But it is perfect for web-oriented template modes (HTML5 and XHTML). It supports and validates the following XHTML specifications:
Standard Dialect processor is the attribute processor that allows browsers to display HTML5/XHTML template files before being processed. It is because they ignore the additional attributes. For example, when a JSP file uses tag library, it includes a fragment of code not displayable by a browser like: The Thymeleaf Standard Dialect allows us to achieve the same functionality with the following code. The above code also allows us to define a value attribute in it (Thomas). The value will be displayed when the prototype is opened in the browser. The attribute will be substituted by the value resulting from the evaluation of ${student.name} during the Thymeleaf processing of the template. It allows a designer and developer to work on the same template file that reduces the efforts required to transform a static prototype into a working template file. It is known as Natural Templating. Thymeleaf Features
Thymeleaf ImplementationWe can implement Thymeleaf template engine by adding spring-boot-starter-thymeleaf dependency in our application's pom.xml file. Spring Boot configures template engine to read template file from /resource/templates. Spring Boot Thymeleaf ExampleLet's create a Spring Boot application and implement Thymeleaf template. Step 1: Open Spring Initializr http://start.spring.io. Step 2: Select the Spring Boot version 2.3.0.M1. Step 2: Provide the Group name. We have provided com.w3cschoool. Step 3: Provide the Artifact Id. We have provided spring-boot-thymeleaf-view-example. Step 5: Add the dependencies Spring Web and Thymeleaf. Step 6: Click on the Generate button. When we click on the Generate button, it wraps the specifications in a Jar file and downloads it to the local system. ![]() Step 7: Extract the Jar file and paste it into the STS workspace. Step 8: Import the project folder in STS. File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-thymeleaf-view-example -> Finish It takes some time to import. Step 9: Create a model class in the package com.w3cschoool. We have created a model class with the name User. In this class, we have defined two variables name and email and generate Getters and Setters. User.java Step 10: Create a controller class. We have created a controller class with the name DemoController. DemoController.java In the next step, we will create the Thymeleaf templates. Step 11: Inside the templates (src/main/resources/templates) folder of the project create a Thymeleaf template with the name user-data. Right-click on the templates folder -> New -> Other -> HTML File -> Next -> Provide the File name -> Finish Note: Do not forget to implement the following in the template file.user-data.html Step 12: Similarly, create an HTML file in the folder templates. We have created an HTML file with the name index. index.html Step 13: Open the application.properties file and add the following properties in it. application.properties After creating all the files, folders, and packages, the project directory looks like the following: ![]() Let's run the application. Step 14: Open the SpringBootThymeleafViewExampleApplication.java file and run it as Java Application. SpringBootThymeleafViewExampleApplication.java Step 15: Now, open the browser and invoke the URL http://localhost:8080. It shows the output, as shown below. ![]() Provide the User Name and Email and click on the Submit button. ![]() After clicking on the Submit button, the URL changes to http://localhost:8080/save and shows the show the user-data, as shown below. ![]() In this section, we have discussed the Thymeleaf view. If we want to make the view more attractive, we can add CSS and JS files in the application. These files must be located under the src/main/resources/static folder. Next TopicSpring Boot Caching
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com