Implementing Spring Boot MVC CRUD operations with JPA and JSP

Photo by

Welcome to another Spring Boot tutorial. If you’re totally new to Spring Boot check out my starter tutorial Setting up your first Spring Boot app on Medium. For this story I’ll be covering how to implement CRUD operations with MVC, JPA and JSP in Spring Boot. Although Spring Boot is popularly being used for building RESTful web services, we can use it for developing MVC structured applications as well, which I will be covering in this story.

In order to get started open your build.gradle or pom.xml file and add the below dependencies. Since I have chosen Gradle as the build tool, my build.gradle file looks as given below. If you’re using Maven make changes accordingly.

Next open the file and add the below properties. For this tutorial I’ll be choosing H2 database and JSP to render views.



Before proceeding with adding the Java classes, create below packages.

// Model classes for mapping database tables
package com.example.demo.model;

After that we need to create a POJO class inside model package as below. This class contains the variables corresponding to the fields exist in the table.

Then we need to create an interface inside repository package for accessing the required data. This might be called as DAO in some other tutorials.

We’ll be using a Service class instead of exposing the Repository interface directly to the Controller class. Refer the Gist below, normally it is where business logic related to an application resides.

After that create a Controller class inside controller package which will act as a gateway between views and business logic. Refer the code snippet below.

We’re done with the backend logic, and need to start working on the user interfaces which will be getting displayed in the browser. Create index.html file inside src/main/resources/static folder and add the below content.

<!DOCTYPE html>
<title>My Contacts - Spring Boot Web MVC</title>
<h1>My Contacts</h1>
<a href="/read-contact">Click here to read all contacts</a>

Next we need to add few JSP pages inside src/main/webapp/WEB-INF/jsp folder. Make sure to create the directory structure if it doesn’t exist.

Inside the folder named above create readcontact.jsp file and add the above content. This code will be used to iterate through the list of contacts provided and display the data in a tabular format.

Next add createcontact.jsp with the Gist above. This page will capture the input when a user needs to create a new contact and send it to backend.

Finally we will create a page for updating a contact. Create updatecontact.jsp referring to the code snippet shared above.

If you’re looking for the source code, check out above GitHub repository. Fork the project and adapt it to your use case, submit a pull request.

Happy Coding! Below is a YouTube video I have published and if you’re interested check out my Medium story on Integrating your Spring Boot project with Amazon S3.

Setting up your first Spring Boot app on YouTube

Software Engineer | AWS Certified | DevOps Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store