This exercise guides you through building the Web Layer (the "Controller" and "View" components) of a Spring Boot application using the MVC (Model-View-Controller) architecture. It focuses on integrating user interface templates with the data layer created in previous steps.
The key objectives of this part of the tutorial are:
Integrating Thymeleaf: Adding the spring-boot-starter-thymeleaf dependency to the pom.xml file to enable the use of HTML templates.
Adding Validation: Including spring-boot-starter-validation to handle data integrity and error checking for user inputs.
Maven Project Reloading: Demonstrating how to refresh the project in IntelliJ IDEA to download and apply these new libraries.
Creating a Main Menu: Developing an index.html file that serves as a landing page with navigation links for listing, adding, updating, and deleting staff.
Displaying Data: Creating a list-staff.html template that uses Thymeleaf attributes (like th:each) to iterate through staff records and display them in a table.
Request Mapping: Creating a StaffController class annotated with @Controller and @RequestMapping("/staffs") to handle specific URL requests.
Handling GET Requests:
Implementing a method to fetch all staff from the repository and pass them to the "list" view.
Creating a method to display the registration form for new staff members.
Handling Data Persistence: Developing an addStaff method that validates input, saves a new staff object to the database via the repository, and redirects the user back to the staff list.
URL Verification: Accessing the application via http://localhost:8080 to view the UI.
Debugging Request Handling: Identifying and resolving "WhiteLabel Error Pages" (404 errors) by ensuring the Controller is correctly configured to handle view requests.