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

loader
Please wait while your application is being created.
Request Callback