14 Spring Boot Thymeleaf
14 Spring Boot Thymeleaf
14 Spring Boot Thymeleaf
What is Thymeleaf?
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine www.thymeleaf.org
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine www.thymeleaf.org
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine www.thymeleaf.org
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine
www.thymeleaf.org
www.luv2code.com
What is Thymeleaf?
• Thymeleaf is a Java templating engine
www.thymeleaf.org
www.luv2code.com
What is a Thymeleaf template?
www.luv2code.com
What is a Thymeleaf template?
www.luv2code.com
What is a Thymeleaf template?
www.luv2code.com
What is a Thymeleaf template?
HTML code
Thymeleaf expressions
www.luv2code.com
What is a Thymeleaf template?
HTML code
Can access
Thymeleaf expressions Java code, objects
Spring beans
www.luv2code.com
Where is the Thymeleaf template processed?
www.luv2code.com
Where is the Thymeleaf template processed?
• In a web app, Thymeleaf is processed on the server
www.luv2code.com
Where is the Thymeleaf template processed?
• In a web app, Thymeleaf is processed on the server
• Results included in HTML returned to browser
www.luv2code.com
Where is the Thymeleaf template processed?
• In a web app, Thymeleaf is processed on the server
• Results included in HTML returned to browser
www.luv2code.com
Hmmm …
www.luv2code.com
Thymeleaf vs JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf vs JSP
• Yes, Thymeleaf is similar to JSP
www.luv2code.com
Thymeleaf Use Cases (non-web)
www.luv2code.com
Thymeleaf Use Cases (non-web)
• Email Template
www.luv2code.com
Thymeleaf Use Cases (non-web)
Hi <<firstName>>,
www.luv2code.com
Thymeleaf Use Cases (non-web)
Hi <<firstName>>,
• CSV Template
www.luv2code.com
Thymeleaf Use Cases (non-web)
Hi <<firstName>>,
• CSV Template
Product,Quantity,Price,Total
…
• Generate a monthly report as CSV then upload to Google drive …
CSV
www.luv2code.com
Thymeleaf Use Cases (non-web)
Hi <<firstName>>,
• CSV Template
Product,Quantity,Price,Total
…
• Generate a monthly report as CSV then upload to Google drive …
CSV
• PDF Template
www.luv2code.com
Thymeleaf Use Cases (non-web)
Hi <<firstName>>,
• CSV Template
Product,Quantity,Price,Total
…
• Generate a monthly report as CSV then upload to Google drive
…
CSV
• PDF Template
• Generate a travel confirmation PDF then send via email Flight: <<flightNum>>
Depart: <<departAirport>>
Arrive: <<arrivalAirport>>
www.luv2code.com
FAQ: Should I use JSP or Thymeleaf?
www.luv2code.com
FAQ: Should I use JSP or Thymeleaf?
• Depends on your project requirements
www.luv2code.com
FAQ: Should I use JSP or Thymeleaf?
• Depends on your project requirements
www.luv2code.com
FAQ: Should I use JSP or Thymeleaf?
• Depends on your project requirements
www.luv2code.com
FAQ: Should I use JSP or Thymeleaf?
• Depends on your project requirements
www.luv2code.com
Thymeleaf Demo
www.luv2code.com
Thymeleaf Demo
www.luv2code.com
Thymeleaf Demo
Output
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Step 1: Add Thymeleaf to Maven pom file
www.luv2code.com
Step 1: Add Thymeleaf to Maven pom file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
www.luv2code.com
Step 1: Add Thymeleaf to Maven pom file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
www.luv2code.com
Step 1: Add Thymeleaf to Maven pom file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Based on this,
Spring Boot will auto configure to
use Thymeleaf templates
www.luv2code.com
Step 2: Develop Spring MVC Controller
www.luv2code.com
Step 2: Develop Spring MVC Controller
File: DemoController.java
@Controller
public class DemoController {
@GetMapping("/")
public String sayHello(Model theModel) {
return "helloworld";
}
}
www.luv2code.com
Step 2: Develop Spring MVC Controller
File: DemoController.java
@Controller
public class DemoController {
@GetMapping("/")
public String sayHello(Model theModel) {
return "helloworld";
}
}
src/main/resources/templates/helloworld.html
www.luv2code.com
Where to place Thymeleaf template?
www.luv2code.com
Where to place Thymeleaf template?
www.luv2code.com
Where to place Thymeleaf template?
www.luv2code.com
Where to place Thymeleaf template?
www.luv2code.com
Step 3: Create Thymeleaf template
www.luv2code.com
Step 3: Create Thymeleaf template
File: src/main/resources/templates/helloworld.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> … </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
www.luv2code.com
Step 3: Create Thymeleaf template
File: src/main/resources/templates/helloworld.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html xmlns:th="http://www.thymeleaf.org">
<head> … </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
www.luv2code.com
Step 3: Create Thymeleaf template
File: src/main/resources/templates/helloworld.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html xmlns:th="http://www.thymeleaf.org">
<head> … </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: src/main/resources/templates/helloworld.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html xmlns:th="http://www.thymeleaf.org">
<head> … </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: src/main/resources/templates/helloworld.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html xmlns:th="http://www.thymeleaf.org">
<head> … </head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: DemoController.java
@Controller
public class DemoController {
@GetMapping("/")
public String sayHello(Model theModel) {
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: DemoController.java
@Controller
public class DemoController {
@GetMapping("/")
public String sayHello(Model theModel) {
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: DemoController.java
@Controller
public class DemoController {
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
Thymeleaf
expression
www.luv2code.com
Step 3: Create Thymeleaf template
File: DemoController.java
@Controller
public class DemoController {
<body>
<p th:text="'Time on the server is ' + ${theDate}" />
</body>
</html>
2
Thymeleaf
expression
www.luv2code.com
Additional Features
www.luv2code.com
Additional Features
• Looping and conditionals
www.luv2code.com
Additional Features
• Looping and conditionals
www.luv2code.com
Additional Features
• Looping and conditionals
www.luv2code.com
Additional Features
• Looping and conditionals
www.thymeleaf.org
www.luv2code.com
CSS and Thymeleaf
Let's Apply CSS Styles to our Page
www.luv2code.com
Let's Apply CSS Styles to our Page
Before
www.luv2code.com
Let's Apply CSS Styles to our Page
Before After
Time on the server is Sat Jan 05 11:42:13 EST 2019 Time on the server is Sat Jan 05 11:42:13 EST 2019
www.luv2code.com
Let's Apply CSS Styles to our Page
Before After
Time on the server is Sat Jan 05 11:42:13 EST 2019 Time on the server is Sat Jan 05 11:42:13 EST 2019
font-style: italic;
color: green;
www.luv2code.com
Using CSS with Thymleaf Templates
www.luv2code.com
Using CSS with Thymleaf Templates
www.luv2code.com
Using CSS with Thymleaf Templates
www.luv2code.com
Using CSS with Thymleaf Templates
www.luv2code.com
Using CSS with Thymleaf Templates
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Step 1: Create CSS file
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
• src/main/resources/static
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
• src/main/resources/static
src/main/resources
static
css
demo.css
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
• src/main/resources/static
src/main/resources
static
File: demo.css
css
.funny {
demo.css font-style: italic;
color: green;
}
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
• src/main/resources/static
src/main/resources
static
File: demo.css
css
.funny {
demo.css font-style: italic;
color: green;
}
Can be any sub-directory name
www.luv2code.com
Step 1: Create CSS file
• Spring Boot will look for static resources in the directory
You can create your own
custom sub-directories
• src/main/resources/static static/css
static/images
static/js
src/main/resources etc …
static
File: demo.css
css
.funny {
demo.css font-style: italic;
color: green;
}
Can be any sub-directory name
www.luv2code.com
Step 2: Reference CSS in Thymeleaf template
www.luv2code.com
Step 2: Reference CSS in Thymeleaf template
File: helloworld.html
<head>
<title>Thymeleaf Demo</title>
</head>
www.luv2code.com
Step 2: Reference CSS in Thymeleaf template
File: helloworld.html
<head>
<title>Thymeleaf Demo</title>
</head>
src/main/resources
static
css
demo.css
www.luv2code.com
Step 2: Reference CSS in Thymeleaf template
File: helloworld.html @ symbol
<head> Reference context path of your application
<title>Thymeleaf Demo</title> (app root)
</head>
src/main/resources
static
css
demo.css
www.luv2code.com
Step 3: Apply CSS
www.luv2code.com
Step 3: Apply CSS
File: helloworld.html
<head>
<title>Thymeleaf Demo</title>
<body>
<p th:text="'Time on the server is ' + ${theDate}" class="funny" />
</body>
www.luv2code.com
Step 3: Apply CSS
File: demo.css
File: helloworld.html
.funny {
<head> font-style: italic;
<title>Thymeleaf Demo</title> color: green;
}
<!-- reference CSS file -->
<link rel="stylesheet" th:href="@{/css/demo.css}" />
</head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" class="funny" />
</body>
www.luv2code.com
Step 3: Apply CSS
File: demo.css
File: helloworld.html
.funny {
<head> font-style: italic;
<title>Thymeleaf Demo</title> color: green;
}
<!-- reference CSS file -->
<link rel="stylesheet" th:href="@{/css/demo.css}" />
</head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" class="funny" />
</body>
www.luv2code.com
Step 3: Apply CSS
File: demo.css
File: helloworld.html
.funny {
<head> font-style: italic;
<title>Thymeleaf Demo</title> color: green;
}
<!-- reference CSS file -->
<link rel="stylesheet" th:href="@{/css/demo.css}" />
</head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" class="funny" />
</body>
www.luv2code.com
Step 3: Apply CSS
File: demo.css
File: helloworld.html
.funny {
<head> font-style: italic;
<title>Thymeleaf Demo</title> color: green;
}
<!-- reference CSS file -->
<link rel="stylesheet" th:href="@{/css/demo.css}" />
</head>
<body>
<p th:text="'Time on the server is ' + ${theDate}" class="funny" />
</body>
www.luv2code.com
Other search directories
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
1. /META-INF/resources
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
1. /META-INF/resources
2. /resources
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
1. /META-INF/resources
2. /resources
3. /static
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
1. /META-INF/resources
2. /resources
3. /static
4. /public
www.luv2code.com
Other search directories
Spring Boot will search following directories for static resources:
/src/main/resources
1. /META-INF/resources
2. /resources Search order: top-down
3. /static
4. /public
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Local Installation
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Local Installation
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Local Installation
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Local Installation
<head>
… …
</head>
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Remote Files
www.luv2code.com
3rd Party CSS Libraries - Bootstrap
• Remote Files
<head>
… …
… …
</head>
www.luv2code.com
Create HTML Tables with Thymeleaf
HTML Tables Start with plain table
Will add CSS in later videos
www.luv2code.com
HTML Tables Start with plain table
Will add CSS in later videos
www.luv2code.com
Big Picture
1
Employee
Controller
Web 2 Model
Browser
Thymeleaf Template
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
1. Create Employee.java
www.luv2code.com
Development Process Step-
By-S
tep
1. Create Employee.java
2. Create EmployeeController.java
www.luv2code.com
Development Process Step-
By-S
tep
1. Create Employee.java
2. Create EmployeeController.java
www.luv2code.com
Step 1: Create Employee.java
www.luv2code.com
Step 1: Create Employee.java
• Regular Java class: fields, constructors, getters / setters
www.luv2code.com
Step 2: Create EmployeeController.java
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
Will add database
integration later
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
return "list-employees";
}
}
www.luv2code.com
Step 2: Create EmployeeController.java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
return "list-employees";
} src/main/resources/templates/list-employees.html
}
www.luv2code.com
Step 3: Create Thymeleaf template
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
<table border="1">
</table>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
<table border="1">
</table>
To Do
Add code to loop over employees
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
<table border="1">
</table>
</body> To Do
Add code to loop over employees
</html>
www.luv2code.com
Step 3: Create Thymeleaf template
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
</tr>
</tbody>
</table>
www.luv2code.com
Thymeleaf and Bootstrap
Let’s Make our Page Beautiful
www.luv2code.com
Let’s Make our Page Beautiful
Before
www.luv2code.com
Let’s Make our Page Beautiful
Before After
www.luv2code.com
Let’s Make our Page Beautiful
Before After
Bootstrap
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Step 1: Get links for remote Bootstrap files
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 2: Add links in Thymeleaf template
www.luv2code.com
Step 2: Add links in Thymeleaf template
<head>
…
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/x.y.z/css/bootstrap.min.css" …>
…
</head>
www.luv2code.com
Step 2: Add links in Thymeleaf template
<head>
…
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/x.y.z/css/bootstrap.min.css" …>
…
</head>
Version number
www.luv2code.com
Step 3: Apply CSS
www.luv2code.com
Step 3: Apply CSS
<body>
<div class="container">
<h3>Employee Directory</h3>
<hr>
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
<h3>Employee Directory</h3>
<hr>
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
</div>
</body>
www.luv2code.com
Create HTML Tables with Thymeleaf
HTML Tables
www.luv2code.com
HTML Tables
www.luv2code.com
HTML Tables Start with plain table
Will add CSS in later videos
www.luv2code.com
Big Picture
Employee
Controller
Web
Browser
Thymeleaf Template
www.luv2code.com
Big Picture
1
Employee
Controller
Web
Browser
Thymeleaf Template
www.luv2code.com
Big Picture
1
Employee
Controller
Web 2 Model
Browser
Thymeleaf Template
www.luv2code.com
Big Picture
1
Employee
Controller
Web 2 Model
Browser
Thymeleaf Template
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Step 1: Create Employee class
www.luv2code.com
Step 1: Create Employee class
• Regular Java class: fields, constructors, getters / setters
www.luv2code.com
Step 2: Create Employee Controller
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
Will add database
integration later
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
return "list-employees";
}
}
www.luv2code.com
Step 2: Create Employee Controller
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/list")
public String listEmployees(Model theModel) {
// create employees
Employee emp1 = new Employee(1, "Leslie", "Andrews", "[email protected]");
Employee emp2 = new Employee(2, "Emma", "Baumgarten", "[email protected]");
Employee emp3 = new Employee(3, "Avani", "Gupta", "[email protected]");
return "list-employees";
} src/main/resources/templates/list-employees.html
}
www.luv2code.com
Step 3: Create Thymeleaf template
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
<table border="1">
</table>
</body>
</html>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
To use Thymeleaf
<!DOCTYPE HTML> expressions
<html lang="en" xmlns:th="http://www.thymeleaf.org">
…
<body>
<h3>Employee Directory</h3>
<hr>
<table border="1">
</table>
</body> To Do
Add code to loop over employees
</html>
www.luv2code.com
Step 3: Create Thymeleaf template
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
www.luv2code.com
Step 3: Create Thymeleaf template
File: list-employees.html
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
Loop over
</tr> list of employees
Loop parameter
</thead>
<tbody>
<tr th:each="tempEmployee : ${employees}">
</tr>
</tbody>
</table>
www.luv2code.com
Thymeleaf and Bootstrap
Let’s Make our Page Beautiful
www.luv2code.com
Let’s Make our Page Beautiful
Before
www.luv2code.com
Let’s Make our Page Beautiful
Before After
www.luv2code.com
Let’s Make our Page Beautiful
Before After
Bootstrap
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Step 1: Get links for remote Bootstrap files
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 1: Get links for remote Bootstrap files
• Visit Bootstrap website: www.getbootstrap.com
www.luv2code.com
Step 2: Add links in Thymeleaf template
www.luv2code.com
Step 2: Add links in Thymeleaf template
<head>
…
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/x.y.z/css/bootstrap.min.css" …>
…
</head>
www.luv2code.com
Step 2: Add links in Thymeleaf template
<head>
…
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/x.y.z/css/bootstrap.min.css" …>
…
</head>
Version number
www.luv2code.com
Step 3: Apply CSS
www.luv2code.com
Step 3: Apply CSS
<body>
<div class="container">
<h3>Employee Directory</h3>
<hr>
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
<h3>Employee Directory</h3>
<hr>
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
</div>
</body>
www.luv2code.com
Step 3: Apply CSS
Bootstrap CSS style
<body>
<div class="container">
</div>
</body>
www.luv2code.com
Thymeleaf CRUD - Real Time Project
m
Application Requirements
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Application Requirements
From the Boss
www.luv2code.com
Real-Time Project Thymeleaf + Spring Boo
t
www.luv2code.com
Real-Time Project Thymeleaf + Spring Boo
t
www.luv2code.com
Big Picture
www.luv2code.com
Big Picture
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1
Employee Employee Employee
Controller Service Repository
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2
Employee Employee Employee
Controller Service Repository
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2 3
Employee Employee Employee
Controller Service Repository
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2 3 4
Employee Employee Employee
Controller Service Repository
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2 3 4
Employee Employee Employee
Controller Service Repository
Web
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2 3 4
Employee Employee Employee
Controller Service Repository
Web 5
Browser
Thymeleaf Templates
www.luv2code.com
Big Picture
1 2 3 4
Employee Employee Employee
Controller Service Repository
Web 5
Browser
Thymeleaf Templates
www.luv2code.com
Application Architecture
www.luv2code.com
Application Architecture Reuse code from
previous project
www.luv2code.com
Application Architecture Reuse code from
previous project
www.luv2code.com
Project Set Up
www.luv2code.com
Project Set Up
www.luv2code.com
Project Set Up
www.luv2code.com
Project Set Up
www.luv2code.com
Project Set Up
• We created all of this code already from scratch … so we'll just copy/paste it
www.luv2code.com
Project Set Up
• We created all of this code already from scratch … so we'll just copy/paste it
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Development Process - Big Picture Step-
By-S
tep
www.luv2code.com
Thymeleaf - Add Employee
Date
Add Employee - DEMO
www.luv2code.com
Add Employee Step-
By-S
tep
www.luv2code.com
Add Employee Step-
By-S
tep
www.luv2code.com
Add Employee Step-
By-S
tep
www.luv2code.com
Add Employee Step-
By-S
tep
www.luv2code.com
Step 1: New "Add Employee" button
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
<a th:href="@{/employees/showFormForAdd}">
Add Employee Add Employee
</a>
www.luv2code.com
Step 1: New "Add Employee" button
@
• Add Employee button will href link tosymbol
Reference context path of your application
(app root)
• request mapping /employees/showFormForAdd
<a th:href="@{/employees/showFormForAdd}">
Add Employee Add Employee
</a>
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
<a th:href="@{/employees/showFormForAdd}">
Add Employee Add Employee
</a>
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
<a th:href="@{/employees/showFormForAdd}"
<a th:href="@{/employees/showFormForAdd}">
class="btn btn-primary btn-sm mb-3">
Add Employee
Add Employee
</a>
</a>
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
<a th:href="@{/employees/showFormForAdd}"
<a th:href="@{/employees/showFormForAdd}">
class="btn btn-primary btn-sm mb-3">
Add Employee
Add Employee
</a>
</a>
Button
Apply Bootstrap styles Button Primary
Button Small
Margin Bottom, 3 pixels
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
• requestDocs on Bootstrap
mapping styles: www.getbootstrap.com
/employees/showFormForAdd
<a th:href="@{/employees/showFormForAdd}"
<a th:href="@{/employees/showFormForAdd}">
class="btn btn-primary btn-sm mb-3">
Add Employee
Add Employee
</a>
</a>
Button
Apply Bootstrap styles Button Primary
Button Small
Margin Bottom, 3 pixels
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to
<a th:href="@{/employees/showFormForAdd}"
<a th:href="@{/employees/showFormForAdd}">
class="btn btn-primary btn-sm mb-3">
Add Employee
Add Employee
</a>
</a>
www.luv2code.com
Step 1: New "Add Employee" button
• Add Employee button will href link to TODO:
Add controller request mapping for
/employees/showFormForAdd
• request mapping /employees/showFormForAdd
<a th:href="@{/employees/showFormForAdd}"
<a th:href="@{/employees/showFormForAdd}">
class="btn btn-primary btn-sm mb-3">
Add Employee
Add Employee
</a>
</a>
www.luv2code.com
Showing Form
www.luv2code.com
Showing Form
In your Spring Controller
www.luv2code.com
Showing Form
In your Spring Controller
• Before you show the form, you must add a model attribute
www.luv2code.com
Showing Form
In your Spring Controller
• Before you show the form, you must add a model attribute
• This is an object that will hold form data for the data binding
www.luv2code.com
Controller code to show form
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
theModel.addAttribute("employee", theEmployee);
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
theModel.addAttribute("employee", theEmployee);
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
theModel.addAttribute("employee", theEmployee);
return "employees/employee-form";
}
…
}
www.luv2code.com
Controller code to show form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
theModel.addAttribute("employee", theEmployee);
return "employees/employee-form";
}
…
} src/main/resources/templates/employees/employee-form.html
www.luv2code.com
Thymeleaf and Spring MVC Data Binding
www.luv2code.com
Thymeleaf and Spring MVC Data Binding
• Thymeleaf has special expressions for binding Spring MVC form data
www.luv2code.com
Thymeleaf and Spring MVC Data Binding
• Thymeleaf has special expressions for binding Spring MVC form data
www.luv2code.com
Thymeleaf Expressions
www.luv2code.com
Thymeleaf Expressions
• Thymeleaf expressions can help you build the HTML form :-)
www.luv2code.com
Thymeleaf Expressions
• Thymeleaf expressions can help you build the HTML form :-)
Expression Description
www.luv2code.com
Thymeleaf Expressions
• Thymeleaf expressions can help you build the HTML form :-)
Expression Description
www.luv2code.com
Thymeleaf Expressions
• Thymeleaf expressions can help you build the HTML form :-)
Expression Description
www.luv2code.com
Thymeleaf Expressions
• Thymeleaf expressions can help you build the HTML form :-)
Expression Description
www.luv2code.com
Step 2: Create HTML form for new employee
www.luv2code.com
Step 2: Create HTML form for new employee
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
Empty place holder
Thymeleaf will handle real work
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
Real work
Empty place holder
Send form data to
Thymeleaf will handle real work
/employees/save
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
Real work
Empty place holder
Send form data to
Thymeleaf will handle real work
/employees/save
</form>
Our model attribute
www.luv2code.com
Step 2: Create HTML form for new employee
www.luv2code.com
Step 2: Create HTML form for new employee
www.luv2code.com
Step 2: Create HTML form for new employee
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
*{…}
Selects property on referenced
th:object
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
*{…}
Selects property on referenced
th:object
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
<button type="submit">Save</button>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
<button type="submit">Save</button>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
1
<form action="#" th:action="@{/employees/save}" When form is loaded,
th:object="${employee}" method="POST"> will call:
<button type="submit">Save</button>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
1
<form action="#" th:action="@{/employees/save}" When form is loaded,
th:object="${employee}" method="POST"> will call:
2
<button type="submit">Save</button>
When form is submitted,
</form> will call:
employee.setFirstName(…)
…
employee.setLastName(…)
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
<button type="submit">Save</button>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
<button type="submit">Save</button>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
</form>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
</form>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
Form control
<input type="text" th:field="*{email}" placeholder="Email">
Apply Bootstrap styles Margin Bottom, 4 pixels
Column Span 4
<button type="submit">Save</button>
</form>
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
Button
<input type="text" th:field="*{firstName}" placeholder="First name"
class="form-control mb-4 col-4"> Button Info
<input type="text" th:field="*{lastName}" Column
placeholder="Last name" Span 2
class="form-control mb-4 col-4">
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
</form>
www.luv2code.com
Step 2: Create HTML form for new employee
<form action="#" th:action="@{/employees/save}"
th:object="${employee}" method="POST">
TODO:
<input type="text" th:field="*{lastName}" placeholder="Last name"
class="form-control mb-4 col-4">
Add controller request mapping for
<input type="text" /employees/save
th:field="*{email}" placeholder="Email"
class="form-control mb-4 col-4">
</form>
www.luv2code.com
Step 3: Process form data to save employee
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
Constructor injection
www.luv2code.com
Step 3: Process form data to save employee
@Controller Since only one constructor
@Autowired is optional
@RequestMapping("/employees")
public class EmployeeController {
Constructor injection
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
www.luv2code.com
Step 3: Process form data to save employee
@Controller
@RequestMapping("/employees")
public class EmployeeController {
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
}
…
For more info see
Redirect to request mapping www.luv2code.com/post-redirect-get
/employees/list
www.luv2code.com
Thymeleaf - Update Employee
Date
Update Employee - Demo
www.luv2code.com
Update Employee Step-
By-S
tep
www.luv2code.com
Update Employee Step-
By-S
tep
www.luv2code.com
Update Employee Step-
By-S
tep
1. "Update" button
www.luv2code.com
Update Employee Step-
By-S
tep
1. "Update" button
www.luv2code.com
Update Employee Step-
By-S
tep
1. "Update" button
www.luv2code.com
Update Employee Step-
By-S
tep
1. "Update" button
www.luv2code.com
Step 1: "Update" Button
www.luv2code.com
Step 1: "Update" Button
www.luv2code.com
Step 1: "Update" Button
Each row has an Update link
When clicked
www.luv2code.com
Step 1: "Update" button
www.luv2code.com
Step 1: "Update" button
• Update button includes employee id
www.luv2code.com
Step 1: "Update" button
• Update button includes employee id
www.luv2code.com
Step 1: "Update" button
• Update button includes employee id
<a th:href="@{/employees/showFormForUpdate(employeeId=${tempEmployee.id})}"
class="btn btn-info btn-sm">
Update
</a>
</td>
</tr>
www.luv2code.com
Step 1: "Update" button
• Update button includes employee id
<a th:href="@{/employees/showFormForUpdate(employeeId=${tempEmployee.id})}"
class="btn btn-info btn-sm">
Update
</a>
Appends to URL
</td>
?employeeId=xxx
</tr>
www.luv2code.com
Step 2: Pre-populate Form
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("employeeId") int theId,
Model theModel) {
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("employeeId") int theId,
Model theModel) {
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("employeeId") int theId,
Model theModel) {
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("employeeId") int theId,
Model theModel) {
www.luv2code.com
Step 2: Pre-populate Form
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("employeeId") int theId,
Model theModel) {
www.luv2code.com
Step 2: Pre-populate Form
www.luv2code.com
Step 2: Pre-populate Form
</form>
www.luv2code.com
Step 2: Pre-populate Form 1
When form is loaded,
will call:
employee.getFirstName()
<form action="#" th:action="@{/employees/save}" …
th:object="${employee}" method="POST">
employee.getLastName
<!-- Add hidden form field to handle update -->
<input type="hidden" th:field="*{id}" />
</form>
www.luv2code.com
Step 2: Pre-populate Form 1
When form is loaded,
will call:
employee.getFirstName()
<form action="#" th:action="@{/employees/save}" …
th:object="${employee}" method="POST">
employee.getLastName
<!-- Add hidden form field to handle update -->
<input type="hidden" th:field="*{id}" />
</form>
www.luv2code.com
Step 2: Pre-populate Form
</form>
www.luv2code.com
Step 2: Pre-populate Form
</form>
www.luv2code.com
Step 2: Pre-populate Form
www.luv2code.com
Step 3: Process form data to save employee
www.luv2code.com
Step 3: Process form data to save employee
• No need for new code … we can reuse our existing code
www.luv2code.com
Step 3: Process form data to save employee
• No need for new code … we can reuse our existing code
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@PostMapping("/save")
public String saveEmployee(@ModelAttribute("employee") Employee theEmployee) {
www.luv2code.com
Thymeleaf - Delete Employee
Date
Delete Employee - DEMO
www.luv2code.com
Delete Employee - DEMO
www.luv2code.com
Delete Employee Step-
By-S
tep
www.luv2code.com
Delete Employee Step-
By-S
tep
www.luv2code.com
Delete Employee Step-
By-S
tep
www.luv2code.com
Delete Employee Step-
By-S
tep
www.luv2code.com
Step 1: "Delete" button
Each row has a Delete button/link
When clicked
- prompt user
- will delete the employee from database
www.luv2code.com
Step 1: "Delete" button
www.luv2code.com
Step 1: "Delete" button
• Delete button includes employee id
www.luv2code.com
Step 1: "Delete" button
• Delete button includes employee id
www.luv2code.com
Step 1: "Delete" button
• Delete button includes employee id
<a th:href="@{/employees/delete(employeeId=${tempEmployee.id})}"
class="btn btn-danger btn-sm"
onclick="if (!(confirm('Are you sure you want to delete this employee?'))) return false">
Delete
</a>
</td>
</tr>
www.luv2code.com
Step 1: "Delete" button
• Delete button includes employee id
Appends to URL
<tr th:each="tempEmployee : ${employees}">
…
?employeeId=xxx
<td>
<a th:href="@{/employees/delete(employeeId=${tempEmployee.id})}"
class="btn btn-danger btn-sm"
onclick="if (!(confirm('Are you sure you want to delete this employee?'))) return false">
Delete
</a>
</td>
</tr>
www.luv2code.com
Step 1: "Delete" button
• Delete button includes employee id
Appends to URL
<tr th:each="tempEmployee : ${employees}">
…
?employeeId=xxx
<td>
<a th:href="@{/employees/delete(employeeId=${tempEmployee.id})}"
class="btn btn-danger btn-sm"
onclick="if (!(confirm('Are you sure you want to delete this employee?'))) return false">
Delete
</a>
</td>
</tr>
JavaScript to prompt user before deleting
www.luv2code.com
Step 2: Add controller code for delete
www.luv2code.com
Step 2: Add controller code for delete
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
www.luv2code.com
Step 2: Add controller code for delete
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/delete")
public String delete(@RequestParam("employeeId") int theId) {
www.luv2code.com
Step 2: Add controller code for delete
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/delete")
public String delete(@RequestParam("employeeId") int theId) {
www.luv2code.com
Step 2: Add controller code for delete
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/delete")
public String delete(@RequestParam("employeeId") int theId) {
www.luv2code.com
Step 2: Add controller code for delete
@Controller
@RequestMapping("/employees")
public class EmployeeController {
…
@GetMapping("/delete")
public String delete(@RequestParam("employeeId") int theId) {
// redirect to /employees/list
return "redirect:/employees/list";
}
…
}
www.luv2code.com