Implementing Spring Boot MVC CRUD operations with JPA and JSP

Afrar Malakooth
3 min readApr 3, 2021

--

Photo by https://dzone.com/articles/how-spring-mvc-really-works

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 application.properties file and add the below properties. For this tutorial I’ll be choosing H2 database and JSP to render views.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix
=.jsp

spring.datasource.url
=jdbc:h2:mem:testdb
spring.datasource.username
=sa
spring.datasource.password
=
spring.datasource.driverClassName=org.h2.Driver

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

// Model classes for mapping database tables
package com.example.demo.model;
// Repository interfaces for accessing database
package com.example.demo.repository;
// Service classes for writing business logic
package com.example.demo.service;
// Controller classes for handling routes
package com.example.demo.controller;

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>
<html>
<head>
<title>My Contacts - Spring Boot Web MVC</title>
</head>
<body>
<h1>My Contacts</h1>
<a href="/read-contact">Click here to read all contacts</a>
</body>
</html>

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 DEV Community video I have published and if you’re interested check out my Medium story on Integrating your Spring Boot project with Amazon S3.

--

--

Afrar Malakooth

An energetic Software Engineer with 5+ years experience of writing code for leading enterprises. Passionate about social work, volunteering and travelling.