Spring MVC

A head start to create a web application using Spring MVC

Chinmaya Sahoo
5 min readDec 21, 2020

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.

Spring MVC workflow

  1. The request will be received by Front Controller i.e. DispatcherServlet.
  2. DispatcherServlet will pass this request to HandlerMapping. HandlerMapping will find suitable Controller for the request
  3. HandlerMapping will send the details of the controller to DispatcherServlet.
  4. DispatcherServlet will call the Controller identified by HandlerMapping. The Controller will process the request by calling appropriate method and prepare the data. It may call some business logic or directly retrieve data from the database.
  5. The Controller will send ModelAndView(Model data and view name) to DispatcherServlet.
  6. Once DispatcherServlet receives ModelAndView object, it will pass it to ViewResolver to find appropriate View.
  7. ViewResolver will identify the view and send it back to DispatcherServlet.
  8. DispatcherServlet will call appropriate View identified by ViewResolver.
  9. The View will create Response in form of HTML and send it to DispatcherServlet.
  10. DispatcherServlet will send the response to the browser. The browser will render the html code and display it to end user.

Advantages Of Spring MVC Framework

  • Separate roles — The Spring MVC separates each role, where the model object, controller, command object, view resolver, DispatcherServlet, validator, etc. can be fulfilled by a specialized object.
  • Light-weight — It uses light-weight servlet container to develop and deploy your application.
  • Powerful Configuration — It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such as from web controllers to business objects and validators.
  • Rapid development — The Spring MVC facilitates fast and parallel development.
  • Reusable business code — Instead of creating new objects, it allows us to use the existing business objects.
  • Easy to test — In Spring, generally we create JavaBeans classes that enable you to inject test data using the setter methods.
  • Flexible Mapping — It provides the specific annotations that easily redirect the page.

Basic functionalities of Spring MVC Annotations

@RequestMapping

@RequestMapping is used to define mapping of web request to handler method or class. @RequestMapping can be used at method level or class level. We will see example of it later.
If you use @RequestMapping annotation at class level, it will be used for relative path for method level path.
Lets understand with the help of example:

@RestController@RequestMapping(value = “/playerController”)public class PlayerController {@RequestMapping(value = “/players”, method = RequestMethod.GET, headers = “Accept=application/json”)public List getPlayers() {List listOfPlayers = playerService.getAllPlayers();return listOfPlayers;}

so web request with URL http://localhost:8080/ProjectName/PlayerController/players will execute getPlayers() method.

Method level RequestMapping:

@RestControllerpublic class PlayerController {@RequestMapping(value = "/players", method = RequestMethod.GET, headers = "Accept=application/json")public List getPlayers() {List listOfPlayers = playerService.getAllPlayers();return listOfPlayers();}

here web request with URL http://localhost:8080/ProjectName/players will execute getPlayers() method.

Class level RequestMapping:

@RestController@RequestMapping(value = "/players")public class PlayerController {@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")public Country addPlayer(@RequestBody Player player) {return playerService.addplayer(player);}@RequestMapping(method = RequestMethod.PUT, headers = "Accept=application/json")public Player updatePlayer(@RequestBody Player player) {return playerService.updatePlayer(player); }}

here, we did not define method level request mapping here, so it will automatically call corresponding PUT or POST method when we enter http://localhost:8080/ProjectName/players

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

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

pom.xml

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

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.

web.xml

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.

spring-servlet.xml

Display the message in jsp page

index.jsp
output

Congratulations. Our very first Spring Hello World application worked successfully.

--

--