Spring MVC?

Aditi Dosi
3 min readNov 8, 2022

--

  1. DispatcherServlet receives the request.
  2. DispatcherServlet dispatches the task of selecting an appropriate controller to HandlerMapping. HandlerMapping selects the controller which is mapped to the incoming request URL and returns the (selected Handler) and Controller to DispatcherServlet.
  3. DispatcherServlet dispatches the task of executing of business logic of Controller to HandlerAdapter.
  4. HandlerAdapter calls the business logic process of Controller.
  5. Controller executes the business logic, sets the processing result in Model and returns the logical name of view to HandlerAdapter.
  6. DispatcherServlet dispatches the task of resolving the View corresponding to the View name to ViewResolver. ViewResolver returns the View mapped to View name.
  7. DispatcherServlet dispatches the rendering process to returned View.
  8. View renders Model data and returns the response.

A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.

A Spring MVC provides an elegant solution to use MVC in spring framework by the help of DispatcherServlet. Here, DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controllers, models, and views.

  • Model — A model contains the data of the application. A data can be a single object or a collection of objects.
  • Controller — A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
  • View — A view represents the provided information in a particular format. Generally, JSP+JSTL is used to create a view page. Although spring also supports other view technologies such as Apache Velocity, Thymeleaf and FreeMarker.
  • Front Controller — In Spring Web MVC, the DispatcherServlet class works as the front controller. It is responsible to manage the flow of the Spring MVC application.

Spring Web MVC Framework Example

Let’s see the simple example of a Spring Web MVC framework. The steps are as follows:

  • Load the spring jar files or add dependencies in the case of Maven
  • Create the controller class
  • Provide the entry of controller in the web.xml file
  • Define the bean in the separate XML file
  • Display the message in the JSP page
  • Start the server and deploy the project

Directory Structure of Spring MVC

Directory Structure of Spring MVC using Maven

Required Jar files or Maven Dependency

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • JSP + JSTL jar files (If you are using any another view technology then load the corresponding jar files).

If you are using Maven, you don’t need to add jar files. Now, you need to add maven dependency to the pom.xml file.

1. Provide project information and configuration in the pom.xml file.

2. Create the controller class

To create the controller class, we are using two annotations @Controller and @RequestMapping.

The @Controller annotation marks this class as Controller.

The @Requestmapping annotation is used to map the class with the specified URL name.

HelloController.java

package com.javatpoint;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class HelloController {

@RequestMapping(“/”)

public String display()

{

return “index”;

}

}

3. Provide the entry of controller in the web.xml file

In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.

4. Define the bean in the xml file

This is the important configuration file where we need to specify the View components.

The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.

This xml file should be located inside the WEB-INF directory.

5. Display the message in the JSP page

This is the simple JSP page, displaying the message returned by the Controller.

index.jsp

<html>

<body>

<p>Welcome to Spring MVC Tutorial</p>

</body>

</html>

--

--