Spring Web MVC Framework Tutorial

Spring MVC Tutorial

Introduction to Spring MVC Framework

The Spring Framework is an open source application framework for Java developers. The Spring Web MVC (Model-View-Controller) component is a part of the larger Spring Framework for building web projects. Using Spring MVC we are able to separate the input logic, business logic and application data. This web framework builds upon the inversion of control (IoC) already present in Spring Framework and allows one to easily build a highly decoupled and customizable web application.

Components of Model-View Controller

  • Model – The model encapsulates the data being managed as a Simple Java Class (POJO) with getters and setters.
  • View – The view is used to generate the output to the user and as generally a JSP, Velocity, FreeMarker or ThymeLeaf page.
  • Controller – The controller processes the requests and builds the appropriate model instance to pass to the view component

Spring Web MVC Framework

Spring MVC framework is built around a main servlet DispatcherServlet, also called Front Controller, that handles request from clients and dispatches to back end components like handler mapping, controller, view resolver and views and sends responses back to clients. To illustrate better please refer to the MVC data flow diagram provided below:

Spring Web MVC Features

  • Clear separation of roles – Each role is fulfilled by different object: DispatcherServlet, HandlerMapping, View Resolver, View, Controller, Validator, etc.
  • Highly Adaptable – Use whatever controller subclass you need for given scenario.
  • Customizable binding and validation – validation errors keep the offending value, localized date and number bindings instead of String-only form objects.
  • Annotation driven configuration option – allows you to use annotations in classes as opposed to xml config only options.
  • Flexible Model Transfer – supports the use of Map for easy integration with other view technologies.
  • Customizable locale, theme resolution, support for JSPs with or without Spring tag library, support for Java Standard Tag Library, support for Velocity without the need for extra bridges, etc.

Spring Web MVC Data Flow

spring mvc flow

In Spring’s Web MVC Framework, the following sequence is used:

  1. Client sends request to access a web page in the form of HTTP request
  2. These HTTP requests are handled by the DispatcherServlet and the HandlerMapping is consulted to find out how to route them
  3. After consulting HandlerMapping, the DispatcherServlet will route to the appropriate controller.
  4. The appropriate controller will process the request and build the ModelAndView instance which it returns to the DispatcherServlet
  5. The DispatcherServlet now consults the ViewResolver to find out what view to route to.
  6. The view is selected and the appropriate page is rendered with the model details nicely displayed
  7. The request is sent back to the client in the form of HTTP response.

Spring Web MVC Framework Examples

With the following examples, we will cover many useful topics of Spring Web MVC Frameworks:

1. Creating Hello World Application using Spring MVC on Eclipse IDE

In this tutorial we will go into some detail on how to set up your Eclipse IDE environment so that you can develop Spring MVC projects. In this post, we will create our first Spring MVC project with the all to familiar “Hello World” sample program.

2. Spring MVC Form Handling Example

The following tutorial will guide you on writing a simple web based application which makes use of forms using Spring Web MVC framework. With this web application you will be able to interact with the customer entry form and enter all of the required values and submit them to the backend processes. I have taken the liberty of using CSS to beautify and transform the HTML page from a standard drab look and feel to a more appealing view.

3. Spring @RequestHeader Annotation Example

In this tutorial, we will discuss the different ways that Spring MVC allow us to access HTTP headers using annotation. We will discuss how to access individual header fields from the request object as well accessing all the headers by supplying Map and then iterating through the LinkedHashMap collection. We will also show you how to set the headers in the response object.

4. Spring MVC Exception Handling using @ExceptionHandler with AngularJS GUI

Good exception handling is a essential part of any well developed Application Framework and Spring MVC is no exception — pardon the pun. Spring MVC provides several different ways to handle exceptions in our applications. In this tutorial, we will cover Controller Based Exception Handling using the @ExceptionHandler annotation above the method that will handle it.

5. Spring RESTful Web Service Example with JSON and Jackson using Spring Tool Suite

For this example, I will be using Spring Tool Suite (STS) as it is the best integrated development environment for building the Spring framework projects. This tutorial will cover setup using Maven on STS and guide you in creating RESTful web services that generate JSON as output using the Jackson libraries.

6. Spring MVC RESTful Web Service Example with Spring Data for MongoDB and ExtJS GUI

This post will show another example of how to build a RESTful web service using Spring MVC 4.0.6, Spring Data for MongoDB 1.6.1 so that we can integrate the web application with a highly efficient datastore (MongoDB 2.6). In this tutorial we will walk you through building the web service and NoSQL database backend and show you how to implement CRUD (Create, Read, Update and Delete) operations.

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *