Spring Web MVC Framework

Dheeraj Kumar
4 min readMar 28, 2021

As the name suggests, it’s a module of the Spring framework dealing with the Model-View-Controller, or MVC pattern. It combines all the advantages of the MVC pattern with the convenience of Spring.

In a nutshell, the DispatcherServlet acts as the main controller to route requests to their intended destination.

The Spring Web MVC framework provides Model-View-Controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.

  • The Model encapsulates the application data and in general they will consist of POJO.
  • The View is responsible for rendering the model data and in general it generates HTML output that the client’s browser can interpret.
  • The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

Dispatcher Servlet

Spring implements MVC with the front controller pattern using its Dispatcher Servlet.

After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.

  • The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.
  • The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request.
  • Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser.

All the above-mentioned components, i.e. HandlerMapping, Controller, and ViewResolver are parts of WebApplicationContext w which is an extension of the plainApplicationContext with some extra features necessary for web applications.

Model

We have a simple model class with a single variable and its getter-setter methods. It’s a simple POJO class.

package com.myApp.spring.model;public class User {
private String userName; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}
}

Controller

The DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controllerannotation indicates that a particular class serves the role of a controller. The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}

The @Controller annotation defines the class as a Spring MVC controller. Here, the first usage of @RequestMapping indicates that all handling methods on this controller are relative to the /hello path. Next annotation@RequestMapping(method = RequestMethod.GET) is used to declare theprintHello() method as the controller’s default service method to handle HTTP GET request. You can define another method to handle any POST request at the same URL.

You can write the above controller in another form where you can add additional attributes in @RequestMapping as follows −

@Controller
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}

The value attribute indicates the URL to which the handler method is mapped and the method attribute defines the service method to handle HTTP GET requests. The following important points are to be noted about the controller defined above −

  • You will define the required business logic inside a service method. You can call another method inside this method as per requirement.
  • Based on the business logic defined, you will create a model within this method. You can use setter different model attributes and these attributes will be accessed by the view to present the final result. This example creates a model with its attribute “message”.
  • A defined service method can return a String, which contains the name of the view to be used to render the model. This example returns “hello” as a logical view name.

Creating JSP Views

Spring MVC supports many types of views for different presentation technologies. These include — JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, Atom and RSS feeds, JasperReports, etc. But most commonly we use JSP templates written with JSTL.

Let us write a simple hello view in /WEB-INF/hello/hello.JSP −

<html>
<head>
<title>Hello Spring MVC</title>
</head>

<body>
<h2>${message}</h2>
</body>
</html>

Here ${message} is the attribute that we have set up inside the Controller. You can have multiple attributes to be displayed inside your view.

Features Of Spring MVC

  1. Clear separation of roles:- Each role like a controller, DispatcherServlet can be fulfilled by a specialized object.
  2. Adaptability, non-intrusiveness, and flexibility:- Define any controller method signature you need. (Such as @RequestParam)
  3. Customizable binding and validation:- Type mismatches as application-level validation errors that keep the offending value, localized date, and number binding, and so on instead of String-only form objects with manual parsing and conversion to business objects.

--

--