Spring MVC and Web Applications
By: Neeraj Takola
Tags: Spring Boot, Web Development, Spring Mvc

In today’s world, every resource, every info is just a click away. With the help of the internet, we can easily get any resource that’s available out there in the world. Web applications or websites give us a response after we request some information. But how does a web application do this functionality? How does it know which resource to look at for our particular request? In this post, we will get a basic knowledge of how a web application works and what role does Spring MVC play in that.

Web Applications

A user or client with the help of a browser surfs the internet. The server located somewhere else checks for all the incoming requests and based on the type of request sends back the response. A client-server architecture is based on this functionality. Web applications are hosted at the server and all its running and execution of tasks is handled by the server.

Spring MVC

Spring MVC is a part of the Spring framework and is a framework for building web applications. It helps the developers by dividing the development of the application into separate parts so that each separation is responsible for a separate concern. Basically, the application is divided into three logical components Model, View, and Controller. Each component is responsible for a specific aspect while developing the application.

The Controller manages the incoming requests and redirects them to the proper resource. It maps the request to a particular method and then switches between going into a model or view.

The Model represents the data of the application. It is independent of the UI or presentation interface. It manages the logic and the data of the application.

The View is responsible for the presentation. It interacts with the client. It is responsible for the interface that is presented to the client. It takes the data from the model and based on the client’s request, presents the data in a meaningful manner.

An example of an office setup can be appropriate for the MVC architecture. When we visit any office, we first go to the reception. The receptionist, based on our requirements, sends us to the appropriate department. Now, the authority at the department, will look into their data and will give us the information. In this scenario, the receptionist becomes the front controller of the setup. The respective department can be thought of as the model that has the data that we need and the information and interaction by the department can be thought of as the view of the system.

How to build an application with Spring MVC?

Creating a web application with MVC architecture is easy and maintainable. Since the application is divided into components so the complexity is highly reduced.

We will try to create an application with the help of Spring Boot. First, we will have to create a new project with the help of the Spring Initializer. The spring initializer will help us set up our project and pulls in all the dependencies required in the project.

Spring Initializer Window

Setting up our project- Part I

Adding dependencies in our project

Before we go any further, we should learn about Maven. Maven is a tool that helps us with all the dependencies in our project. It pulls all the dependencies from a central maven repository and stores them in our local machines. These dependencies then can be further included in our other projects.

All this steps are enough for the application to run. We haven’t included any functionality yet but for time being lets run the project.

Server logs of the application

We have to add hibernate specific configuration for project so that we can get and save the data in the database. A demo database is used in the example. This data source configuration is saved in the resources->application.properties file.

application.properties window

We will create one user model class and we will try to get user registered and then we will give them the access to our system. All these requests will be handled by our user controller.

User Class

User Controller

The @RequestMapping , @GetMapping , and @PostMapping maps the Http request to the controller’s methods. Then either the view is shown to the user or the request is redirected to some other source for further functioning.

Registering the user.

The login page

Validating the registered user.

All these steps are more than enough to create a web application. For further reading and information, please click here. Keep learning!