Spring MVC - 9. CRUD [Update]

Share

Summary

This tutorial focuses on implementing the update (U in CRUD) functionality for clubs in a Spring MVC application. It covers creating GET and POST endpoints for editing, handling club IDs, mapping data transfer objects (DTOs) to entities, and updating club information in the database. The video also touches on integrating an edit button into the club list and prepares for future validation implementation.

Highlights

Setting up the GET Endpoint for Editing
00:00:21

The video starts by explaining the need for update functionality. It then details the creation of a GET endpoint for editing clubs, using `/clubs/{clubId}/edit`. This involves a `Path variable` to capture the club ID and a `ClubService.findClubById` method to retrieve the club's data. The process includes generating the `findClubById` method and mapping the entity to a DTO for display.

Configuring the Edit HTML Page
00:04:47

The next step focuses on creating the `edit.html` template. This involves copying the existing `create.html` and modifying it. Key changes include updating the form's `th:action` to reflect the edit endpoint, incorporating the club ID into the URL, and adding a hidden input field for the club ID to ensure it's passed during the update.

Implementing the POST Endpoint for Updates
00:06:48

This section covers the implementation of the POST endpoint for updating club data. It uses `/clubs/{clubId}/edit` and takes both the club ID (from the path variable) and a `ClubDto` (from the model attribute). The tutorial emphasizes setting the club ID in the DTO before calling `ClubService.updateClub`. It also details the creation of the `updateClub` method in the service layer, which involves mapping the DTO back to a club entity and saving it to the repository.

Adding an Edit Button to the Club List
00:12:06

The video demonstrates how to add an 'Edit' button to each club listing on the `clubs.html` page. This button uses `th:href` to navigate to the edit page for the specific club, dynamically including the club's ID in the URL. A quick test shows the update functionality in action, highlighting the need for validation in future updates due to uncontrolled input.

Recently Summarized Articles

Loading...