Spring MVC Tutorial: A Complete Guide For 2021

Introduction 

Welcome to the Spring MVC tutorial. If you are a fan of Java frameworks, then it is highly probable that you are familiar with the word Spring. For building web applications,  spring is the popular Java framework. As it does not require activation of web server, any web application can easily integrate with Spring framework. This article will give you a decent knowledge about the Spring MVC tutorial and also related topics.

In this article about Spring MVC tutorial, let us look at:

  1. What is Spring MVC? 
  2. Spring Web model- view- controller 
  3. Workflow of Spring MVC 
  4. Spring Web MVC Framework example
  5. Advantages of Spring MVC framework
  6. Spring MVC architecture

1. What is Spring MVC? 

Spring MVC is a  framework of Java that is generally used in order to develop web applications. Model view controller pattern is followed by Spring MVC. All the primary features of the mainspring framework such as dependency injection,  control inversion are also implemented by spring MVC.  It also provides an effective way to utilize MVC with the use of Dispatcher Servlet. Move on to the article further to know in detail.

Spring MVC framework

Spring MVC framework gives pre-built elements for all levels of web application architecture, and the developer just needs to integrate them.

Front Controller

This is the Dispatcher servlet controller, also known as the Front controller, which is the primary controller that governs the application’s spring MVC flow.

This is defined in the web.xml:

  1. <web-app>
  2.     <servlet>
  3.         <servlet-name>Dispservlet</servlet-name>
  4.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5.         <load-on-startup>1</load-on-startup>
  6.     </servlet>
  7.     <servlet-mapping>
  8.         <servlet-name> Dispservlet </servlet-name>
  9.         <url-pattern>/</url-pattern>
  10.     </servlet-mapping>
  11. </web-app>

 

So Dispatcher servlet is constructed and linked to the URL pattern /, which indicates that all requests must pass through it.

Controller

The Controller handles the particular request linked by request mapping.

Example

  1. @Controller
  2. Public class ABC{
  3. @RequestMapping(“/Submit”)
  4. Public void Submit(){}

The Controller class is defined with @controller, and every method is linked to some request URL.

Model

The model is the class that contains data that is sent between the view and the controller.

The model class transmits anything in the view to the controller and vice versa.

View Template

A view template is a UI element used to show data, and it can be JSP or another view technology.

Based on the view resolver, a specific view for the relevant request is provided, which assists in determining the right view name.

2. Spring Web model: view-controller 

It consists of four important elements. They are Model, Controller, View, and Front Controller. Know in detail from below : 

1) Model: The model consists of the main data of the application. Such data can either be a group or it may be a  sole object.

2) Controller:  The Controller generally consists of the business reasoning the application. In order to spot the  Controller class,  one can use @Controller annotation.

3) View: Generally, to represent the required information in a specific given format, View is used so far. Mostly, JSP JSTL is utilized to design a view page. Also, view technologies like Freemaker and Apache velocity are also even supported by the spring.

4) Front Controller: The Dispatcher Servlet functions as the front controller in the spring web model view controller. This is also responsible in managing the flow of the application.

3. Workflow of Spring MVC 

The following explains the workflow of spring MVC :

  • Dispatcher Servlet which functions as the front controller obstructs all the arriving requests. 
  • Dispatcher Servlet then gets an appearance of handler mapping in turn from the file of XML. Next, it forwards it to the controller.
  • Thereby, a return of the Model and view object is done by the controller.
  • At last, the Servlet verifies the entry of the view and thereafter entreats the view component.

This is the flow of spring MVC in brief.

4. Spring Web MVC Framework example 

In order to create an example of the Spring MVC framework, these steps should be followed. 

  • Firstly you should load the jar files of spring or in the case of Maven, you should sum dependencies. 
  • The second step is to create a controller class.
  • Give the respective entry of the controller in the web XML file.
  • Thereafter you should explain the bean in the different XML file.
  • Show the message on the JSP page.
  • Finally you should start the server and install the project.

Needed Jar files or Maven Dependency 

In order to run the example, one should load 

•    Spring jar files 

•    Spring Web jar files

•    Java server page (JSP) file and Java standard Tag Library (JSTL ) file. ( If you are involved in any other technical view, then you may load respective jar files)

If you are likely in using Maven, then you do not need any jar file addition. But you are bound to sum up  Maven dependency to the pom.xml file.

1)    Enter the info in the pom.xml file 

You are required to enter the information of the project and configuration in the pom.xml   file.

2)   Design the controller class 

You can use any annotation but here two annotations @ controller annotation and @ RequestMapping are preferred.

The @controller annotation is used to mark the class.

The @RequestMapping is used in order to map class with a particular URL name.

3)    Enter controller in web.xml file.

We are required to set Servlet class Dispatcher Servlet which functions as front controller into this XML file.  Thus all the requests incoming will be headed and forwarded towards Dispatcher Servlet.

4)   Define bean in the XML file 

This is the key configuration file and here you need to set view components.  And also this XML  must be situated inside WEB-INF Directory.

5)    Show or Exhibit the message on the JSP page

You should exhibit the same message in the java server page file.

A proper Spring MVC tutorial would be remiss if we didn’t address its merits:

5. Advantages of Spring MVC framework

  • Rapid fast development:  It facilitates super-fast development in a hassle-free manner. 
  • Lightweight: In order to develop and install your required application, it uses a very light Servlet, thus making it so feasible. 
  • Easy in Testing:  Generally in spring, Java bean classes are created which enables you to induce test data by the use of setter methods. Thus it makes testing so easy and complex free and comfortable. 
  • Adaptable Mapping:  Providing the different specific annotations which easily redirect the page, it makes the mapping flexible. 
  • Discrete roles: The spring MVC part and separate each and every role where the controller,  view resolver, model object and command object can be satisfied by a specialized object.
  • Powerful configuration and business code which can be reused: It provides a strong configuration for application and framework classes. You can also utilize the present business objects instead of creating new ones.

6. Spring MVC architecture

Web MVC frameworks add abstractions on which layer of MVC architecture JSP are deployed, making it easier to construct MVC-based applications. There are several open-source MVC frameworks available, some of which include:

  • Struts from Apache
  • WebWork from Open Symphony
  • JSF from Sun MS
  • Spring Web MVC

So, in the core, Spring MVC is a spring model view controller architecture, a tested architectural method for developing web-based applications. 

Conclusion

This brings us to the end of this Spring MVC tutorial. Even in the present era, Spring is still relevant and it is advisable to learn and get knowledge through Spring MVC tutorial.

If you are interested in learning more about software development, you can check out our holistic Postgraduate Certificate Program in Full Stack Development.

ALSO READ

 

Related Articles

} }
Request Callback