Spring MVC - 8. CRUD [Create]

Share

Summary

This video shows how to implement a create function in Spring MVC using forms, GET and POST requests, and Thymeleaf for dynamic content.

Highlights

Understanding Forms and Data Submission
00:00:00

Forms are essential for creating data in web development. HTML form tags and input tags are used to collect data, which is then converted into JSON and submitted to a server. Thymeleaf is used for special syntax, where 'action' defines the POST endpoint and 'object' represents the model to be sent as JSON.

Creating a GET Request to Display the Form
00:01:21

To display the form, a GET request is created in the controller at the /clubs/new endpoint. A method named 'createClubForm' is implemented to render the view, passing a new Club object as a model attribute to avoid errors. This sets up the template for the form.

Designing the Form with Bootstrap
00:02:46

A form template is created by copying an existing template and customizing it. Bootstrap forms are used for a presentable layout. After initial setup, unnecessary form elements like city, zip, and a 'check me out' checkbox are removed. A container class and margin-bottom are added for better aesthetics and styling.

Implementing the POST Request for Form Submission
00:05:12

A POST request is created at the /clubs/new endpoint to handle form submissions. The 'saveClub' method is implemented in the Club service and repository to persist the new club data. Upon successful saving, the user is redirected to the main clubs page.

Wiring Up Form Fields with Thymeleaf
00:07:11

The form is configured with 'th:action' and 'th:object' to link it to the POST endpoint and the Club object. Input fields are then mapped to properties like 'title', 'photo URL', and 'content' using 'th:field' for data binding. This ensures the data from the form fields is correctly submitted.

Testing the Form Submission
00:09:28

A debugger is used to verify that the form data is correctly sent to the controller. An example Club with a title, photo URL, and content is created. The debugger confirms that the Club object, including generated ID and timestamps, is successfully returned after saving.

Adding a Link to the Create Form
00:10:43

Finally, a link is added to the application's layout using 'th:href' to allow users to navigate to the 'Create Running Club' form. This completes the full CRUD create functionality, enabling users to add new club entries.

Recently Summarized Articles

Loading...