Tutorial 4
Tutorial 4
Contents:
Continue the project from Tutorial 3
Practice the one-to-many relationship between data entities in Spring Boot
Establish web template using Thymeleaf layout dialect
SE2 – TUTORIAL 2
Implement One-To-Many relationship:
1. Add new entity (Company) then add a proper annotations to represent the relationship
between Company and Employee. In Company, the relationship is @OneToMany and is
represented by the employees attribute as shown in the figure below. In Employee,
the relationship is @ManyToOne and is represented by the company attribute. Don’t
forget to create getter method for employees attribute and getter & setter methods for
company attribute.
SE2 – TUTORIAL 2
Figure 1: Company.java
Figure 2: Employee.java
SE2 – TUTORIAL 2
4. Create CRUD features for Company. You need to add these controller methods:
The /company/list URL triggers getAllCompany() controller method, which
uses the companyList.html template.
The /company/add URL triggers addCompany() controller method, which uses
the companyAdd.html template.
The /company/{id} URL triggers getCompanyById() controller method, which
uses the companyDetail.html template. This page should show a list of
employees in the company.
The /company/update/{id} URL triggers updateCompany() controller method,
which uses the companyUpdate.html template.
The /company/delete/{id} URL triggers deleteCompany() controller method,
which redirects to the /company/list page after deleting the company.
(*) Refer to previous tutorials to learn how to code CRUD features for an entity.
5. Update EmployeeController to change URLs of employee CRUD features and to
add CompanyRepository so that you can choose an employee’s company when
adding a new employee.
SE2 – TUTORIAL 2
6. Update the template employeeAdd.html to add a <select> element (drop-down list)
for selecting company.
SE2 – TUTORIAL 2
<a href="/company/list">Company List</a>
<a href="/company/add">Add Company</a>
</nav>
<div layout:fragment="content">
<!-- content page will override this -->
</div>
</body>
</html>
This layout contains a navigation bar with all the links to other pages. This navigation bar
is meant to be displayed on top of every page. This layout also contains a fragment area
named content which will be filled up by any template that decorates this layout.
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="_layout">
<head>
<meta charset="UTF-8">
<title>Employee List</title>
</head>
<body>
<div layout:fragment="content">
<h2>EMPLOYEE LIST</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
<th>Update</th>
<th>Delete</th>
</tr>
<tr th:each="employee : ${employees}">
<td th:text="${employee.id}"/>
<td>
<a th:href="'/detail/' + ${employee.id}" th:text="${employee.name}"/>
</td>
<td>
<img th:src="${employee.image}" width="100" height="100"/>
</td>
<td><a th:href="'/update/' + ${employee.id}">UPDATE</a></td>
<td><a th:href="'/delete/' + ${employee.id}">DELETE</a></td>
</tr>
</table>
</div>
</body>
</html>
SE2 – TUTORIAL 2
4. Edit other templates (employeeAdd, employeeDetail, employeeUpdate) similarly to the
way you editted the employeeList template.
SE2 – TUTORIAL 2