Releases: NicolasPR-BR/SpringBoot
Section 7 Example MVC program
What this example is about
This is a Spring Boot MVC Application that performs simple, yet, critical operations on a database.
This example creates a server that serves a webpage listing multiple employees, on this page you have the following options:
- To create a new employee
- To update an employee
- To delete an employee
List
Simple, employeeService uses EmployeeRepository to list all employees within the SQL database, it then adds the employees to the "list-employees" model as an array of employees. The code iterates through the array adding each employee to a row in the table.Create and Update buttons from list-employees page
![]() |
![]() |
---|
The "Create" and "update" pages are the same, the only real change is in the behavior of the controller.
When the user clicks the "update" button, we make a GET request to "/showFormUpdate", the controller will retrieve that employee and add it to the page model of "employee-form" which will be displayed, when the form is submitted, the employee will already have an ID, the controller will get the POST request on "/save", since an ID already exists within the JSON request coming from the browser, JPA will simply update that employee and won't create another one.
If the user clicks the "add a new employee" button, no data would come with the "employee-form" page, and when submitted, an "id" won't already exist (it'll be 0 or null), in this case, JPA would create another employee in the database.
Delete
This is really simple process, a GET request will be made to "/delete{employeeId}", the controller will get the ID, call employeeService and delete it.
To run this example
You must do the following:
- Have mysql installed
- Create a user in your database called "springstudent" with the password "springstudent"
- Must have run the sql-script found in: "section7-SpringMvc-thymeleaf/01-thymeleaf-demo-employees-starter-list/sql-scripts/"
- Download this file and run it with "java -jar".