Laboratory Work: 4 Learning Spring Boot and Thymeleaf Prelab Work
Laboratory Work: 4 Learning Spring Boot and Thymeleaf Prelab Work
Laboratory Work: 4 Learning Spring Boot and Thymeleaf Prelab Work
1- What is Thymeleaf?
Thymeleaf is a Java XML/XHTML/HTML5 Template Engine that can work both in web
(Servlet-based) and non-web environments. It is better suited for serving XHTML/HTML5 at the
view layer of MVC-based web applications, but it can process any XML file even in offline
environments. It provides full Spring Framework integration.
The template file of Thymeleaf in essence are only a ordinary document file with the format
of XML/XHTML/HTML5. Thymeleaf Engine will read a template file and combine
with Java objects to generate another document.
Thymeleaf can be used to replace JSP on the View Layer of Web MVC application. Thymeleaf is
open source code software, licensed under the Apache 2.0.
Below is the image of the application that we will perform in this lesson:
2- Create application
On Eclipse/Intellij IIDEA select:
File/New/Other...
Enter:
Name: SpringBootThymeleaf
Group: org.o7planning
Artifact: SpringBootThymeleaf
Description: Spring Boot and Thymeleaf
Package: org.o7planning.thymeleaf
Select 2 Web and Thymeleaf technologies.
Your Project has been created:
pom.xml
?
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6 <modelVersion>4.0.0</modelVersion>
7
8 <groupId>org.o7planning</groupId>
9 <artifactId>SpringBootThymeleaf</artifactId>
10 <version>0.0.1-SNAPSHOT</version>
11 <packaging>jar</packaging>
12
13 <name>SpringBootThymeleaf</name>
14 <description>Spring Boot and Thymeleaf</description>
15
16 <parent>
17 <groupId>org.springframework.boot</groupId>
18 <artifactId>spring-boot-starter-parent</artifactId>
19 <version>2.0.0.RELEASE</version>
20 <relativePath/> <!-- lookup parent from repository -->
21 </parent>
22
23 <properties>
24 <project.build.sourceEncoding>UTF-8</
25 project.build.sourceEncoding>
26 <project.reporting.outputEncoding>UTF-8</
27 project.reporting.outputEncoding>
28 <java.version>1.8</java.version>
29 </properties>
30
31 <dependencies>
32 <dependency>
33 <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</
34
artifactId>
35
</dependency>
36
<dependency>
37
<groupId>org.springframework.boot</groupId>
38
<artifactId>spring-boot-starter-web</artifactId>
39
</dependency>
40
41
<dependency>
42
<groupId>org.springframework.boot</groupId>
43
<artifactId>spring-boot-starter-test</artifactId>
44 <scope>test</scope>
45 </dependency>
46 </dependencies>
47
48 <build>
49 <plugins>
50 <plugin>
51 <groupId>org.springframework.boot</groupId>
52 <artifactId>spring-boot-maven-plugin</
53 artifactId>
54 </plugin>
55 </plugins>
</build>
</project>
3- Thymeleaf Template
Thymeleaf Template is a template file. Its contents are in the XML/XHTML/HTML5 format. We
will create 3 files and place it in the src/main/resources/templates folder:
index.html
?
1 <!DOCTYPE HTML>
2 <html xmlns:th="http://www.thymeleaf.org">
3 <head>
4 <meta charset="UTF-8" />
5 <title>Welcome</title>
6 <link rel="stylesheet" type="text/css"
7 th:href="@{/css/style.css}"/>
8 </head>
9 <body>
10 <h1>Welcome</h1>
11 <h2 th:utext="${message}">..!..</h2>
12
13 <!--
14
15 In Thymeleaf the equivalent of
16 JSP's ${pageContext.request.contextPath}/edit.html
17 would be @{/edit.html}
18
19 -->
20
21 <a th:href="@{/personList}">Person List</a>
22
23 </body>
24
25 </html>
personList.html
?
1 <!DOCTYPE HTML>
2 <html xmlns:th="http://www.thymeleaf.org">
3 <head>
4 <meta charset="UTF-8" />
5 <title>Person List</title>
6 <link rel="stylesheet" type="text/css"
7 th:href="@{/css/style.css}"/>
8 </head>
9 <body>
10 <h1>Person List</h1>
11 <a href="addPerson">Add Person</a>
12 <br/><br/>
13 <div>
14 <table border="1">
15 <tr>
16 <th>First Name</th>
17 <th>Last Name</th>
18 </tr>
19 <tr th:each ="person : ${persons}">
20 <td th:utext="${person.firstName}">...</td>
21 <td th:utext="${person.lastName}">...</td>
22 </tr>
23 </table>
24 </div>
25 </body>
26 </html>
addPerson.html
?
1 <!DOCTYPE HTML>
2 <html xmlns:th="http://www.thymeleaf.org">
3 <head>
4 <meta charset="UTF-8" />
5 <title>Add Person</title>
6 <link rel="stylesheet" type="text/css"
7 th:href="@{/css/style.css}"/>
8 </head>
9 <body>
10 <h1>Create a Person:</h1>
11
12 <!--
13 In Thymeleaf the equivalent of
14 JSP's ${pageContext.request.contextPath}/edit.html
15 would be @{/edit.html}
16 -->
17
18 <form th:action="@{/addPerson}"
19 th:object="${personForm}" method="POST">
20 First Name:
21 <input type="text" th:field="*{firstName}" />
22 <br/>
23 Last Name:
24 <input type="text" th:field="*{lastName}" />
25 <br/>
26 <input type="submit" value="Create" />
27 </form>
28
29 <br/>
30
31 <!-- Check if errorMessage is not null and not empty -->
32
33 <div th:if="${errorMessage}" th:utext="${errorMessage}"
34 style="color:red;font-style:italic;">
35 ...
36 </div>
37
38 </body>
</html>
I have created 3 HTML files above. The above HTML files need to be appropriate to the standards
of XML. All tags must have open and close, for example:
?
1 <div>A div tag</div>
2
3 <br />
4
5 <meta charset="UTF-8" />
All HTML files need declaring use of Thymeleaf Namespace:
?
1 <!-- Thymeleaf Namespace -->
2
3 <html xmlns:th="http://www.thymeleaf.org">
In template files, there are Thymeleaf Markers (markers of Thymeleaf) which are instructions
helping Thymeleaf Engine process data.
Thymeleaf Engine analyses template files and combines them with Java data to generate a new
document.
Below are examples to use the Context-Path in the Thymeleaf:
?
1 <!-- Example 1: -->
2
3 <a th:href="@{/mypath/abc.html}">A Link</a>
4
5 Output: ==>
6
7 <a href="/my-context-path/mypath/abc.html">A Link</a>
8
9
10 <!-- Example 2: -->
11
12 <form th:action="@{/mypath/abc.html}"
13 th:object="${personForm}" method="POST">
14
15 Output: ==>
16
17 <form action="/my-context-path/mypath/abc.html" method="POST">
18
http://localhost:8080/