Nowadays, developers tend to move towards frameworks than using the actual Language itself for development. The reason is their simplicity as well as their power of development.
Considering Spring, it is totally a monster of frameworks! Spring has many advantages; Spring provides a lightweight container that can be activated without using a web server or application server software. It gives good support for IoC (Inversion of Control) and Dependency Injection results in loose coupling. With that, the interaction with the Database becomes a piece of cake!
In this tutorial, we are gonna develop a site in our local server that supports user registration and login using spring boot. We are going to follow MVC (Model — View — Controller) architecture in this tutorial.
- Knowledge in Core Java
- Coding skills
- Knowledge in Servlet and JSP
- Java Development Kit (JDK)
- Spring tool suite 4 IDE — open-source (recommended)
- MySQL database — You can get it here !
- A good browser (Chrome — recommended)
Make sure you have all the listed prerequisites software installed and have basic knowledge on the request-response cycle as well as web containers. What more? Let’s jump into the tutorial!
- Open Spring Tool Suite application
- Navigate to your workspace
- Click File -> New -> Project ->Spring Starter Project
- Give the essential data in the opened dialog box
- The specifications given for my project is listed below
The dependencies I am adding to this project are given below. You can either add them during the creation of the project itself or later search for them in Maven repository and add the tags of the required version in the pom.xml file.
- Spring boot starter web
- Tomcat embed jasper
- Spring boot starter data jpa
- Mysql connector java
- Spring boot starter test
The hierarchy is the important thing to notice in the MVC design structure. My project hierarchy is as below.
Model — View — Controller
Here we shall see a brief on MVC architecture. Simply, it is a method of arranging our files based on their functionality. Rather than creating all our files in the same package, we shall split it and store it to achieve a great architecture for our project to make our code easily maintainable.
Model — The file that stores the model or a structure of the objects that we are gonna deal with in our project will be stored here. In our project, it contains the User class.
View — The files that are responsible for the content that is shown in the screen of the client are stored here. It mostly contains the template files of a project.
Controller — The file that is responsible for the navigation of the site and controls the tasks running in the back end are stored here.
Packages in our project
The newly created project will be having a package of your group name.firstapp in src/main/java which contains the main class. There are no modifications in the class because it contains the main method. In this package we are gonna create four more inner packages:
controller package for having the files that control the server
model package for having User class
services package for executing tasks on users with repository
repository package to contain and work on users repository
In src/main we are adding a folder webapp, that further contains the folder WEB-INF, that contains the folder ‘view‘. The view folder is to keep all jsp files that are displayed to the users.
Application Properties Configuration
In src/main/resources there is file called application.properties which is responsible for the database connectivity and view resource locating. First two lines set the view folder as the default folder to fetch views for our site. Other lines are responsible to connect the MySQL database to the server. The configuration is as below:
The project repository that I have done can be found in this bitbucket link. You can follow up the tutorial to find the explanation for the code.
It contains the interface for user repository, whose object is created at runtime for usage. It will be supporting CRUD operations in the database by implementing the
crudRepository interface. It has an abstract method
findByUserNameAndPassword() that fetches user with given user name and password. Annotations used are as below –
@Repository — specifies that this interface will be defining the repository for the project
It contains the UserService class that contains a field of
UserRepository object. All the database operations like saving and fetching the users are initiated from here and performed through its data field and member methods. Annotations used are as follows –
@Service — Represents that this class elicits users service
@Transactional — Represents that the class methods perform transactions over the database.
This contains the User class that gives the object for the users. It accepts name, username and password to create a user. The annotations used are as follows –
@Entity — It specifies that the class is an entity and is mapped to a database table.
@Table — It specifies the table name to which the entity is mapped
@Id — It specifies that the property is the Id in the table
@Column — It species that the property is a column in table with the specified name.
This package contains the
AppController class that decides that decides on which method to be called when and how. The requests come to the controller and it is processed by the controller. The string returned by its methods are mapped to views to elicit a response. When users login, session attributes are set here. The annotations used are as follows-
@Controller — It depicts that the class is the controller part of the application
@Autowired — It is for the Inversion of Control. The control of dependency for the project ‘UserService’ object is injected or inversed to the application. This is called dependency injection
@RequestMapping — It specifies that the method should be called when the given URL pattern is encountered.
It contains the JSP files to decide the view of the clients. The JSP files get session attributes and request object parameters set by the controller to decide on what to show when. It contains two JSP files, one to show before login and another to show after login.
Hurray! We have completed the coding portion for our application. Now let’s run this application.
Running the application
Right click the root folder of the application -> Run As -> Spring Boot App
If the console didn’t prompt any error, proceed to the browser and go to the localhost using the default port 8080 as
localhost:8080 or 127.0.0.1:8080
Great! Our application is working fine! Now users can be registered and logged in with our site. The screenshots of registration and login are as follows –
Hope you had a great session with us! Follow us for such great and awesome tutorials !