After years of programming with Spring MVC, now I’m quite confident to share with you, fellow programmers, some introductory information about Spring Web MVC, or simply Spring MVC. I hope you will find this article helpful, for getting started with Java web development with Spring - the world’s most popular Java framework.

 

1. What is Spring MVC?

In short, Spring MVC is a web framework that is built on Java Servlet stack. The formal name is “Spring Web MVC”, but it is more commonly known as “Spring MVC”.

Spring MVC is included in Spring framework in the very beginning of Spring platform and ecosystem. It is designed around Front Controller and Model View Controller (MVC) design patterns, hence the name Spring MVC.

Today, Spring is a part of Spring Framework Core, which is a sub project under the Spring platform’s umbrella.


2. Why is Spring MVC used?

Developers use Spring MVC for creating web applications running on Servlet containers (a kind of web server that hosts websites written in Java programing language) such as Apache Tomcat.

Spring MVC is built to simplify and enrich the development of Java web apps based on Servlet stack, as compared to the development with Java Servlet alone.

So you can use Spring MVC to develop web apps running on Servlet containers on Java platform, for:

  • traditional HTTP request/response model
  • asynchronous request/response model with Servlet Async API
  • RETSful web services and REST APIs


3. Key Features of Spring MVC

Spring MVC inherits the core features of Spring framework such as dependency injection, AOP, internalization, Spring Expression language (SpEL)… plus the key features that simplify and enrich development of web applications running on Java servlet containers:

  • Dispatcher Servlet (a kind of front controller), handler mapping, annotated controllers
  • Form handling & Form validation
  • View resolution; resolvers for exception, locale, theme, multipart file upload, logging…
  • Integration with view technologies such as Thymeleaf, FreeMarker, JSP, JSTL, PDF, Excel…
  • Integration with Spring Security
  • Integration with Servlet’s asynchronous processing


4. How does Spring MVC work?



To understand how Spring MVC works, firstly you should understand the Front Controller and Model View Controller (MVC) design patterns, which are the philosophy of Spring MVC.The following picture illustrates the concept of Front Controller design pattern:

front controller design pattern

In design pattern, all incoming requests are handled by a single piece of code called front controller, which then delegates the requests to be processed by further application objects.

And the following diagram explains the concepts of Model View Controller or MVC design pattern:

mvc design pattern

In this design pattern, the application’s code is segregated into 3 logical layers: Model, View and Controller. The model represents application’s data; the View renders user interface; and the Controller handles incoming requests, invokes application’s services that process data, and updates the Model and selects appropriate view for the request.

And the following pictures describes the basic architecture of Spring MVC:

spring mvc architecture

The heart of Spring MVC is Dispatcher Servlet which is a front controller. The dispatcher servlet invokes several objects for processing a single request: handler mapping, controller, view resolver and view. Let me explain how it works in sequence:

  • a handler mapping determines which URL is handled by which handler method in which controller class.
  • a controller takes the request, reads inputs, invokes business objects that process the inputs, and selects a logical view name for the request.
  • the view resolver determines which view name maps to which physical view template (HTML, JSP, Excel…)
  • the controller picks the appropriate view template, passes model data and generates response.


5. Learn more about Spring MVC

To understand more in-depth about Spring MVC, I recommend you read this article: Understand Spring MVC. Furthermore, use the following resources:

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment