Web MVC frameworks provide abstraction layer on core servlet, jsp technologies, and simplifies the process of developing MVC architecture based applications. There are many MVC frameworks available open source and some of them include:

  •  Struts from Apache
  • JSF from Sun MS (Oracle Corp)
  • WebWork from Open Symphony
  • Spring Web MVC

So basically MVC is a Model-View-Controller architecture, a proven design solution to develop web based applications. Spring as an MVC framework provides pre-built components, for all layers of web application architecture, and the developer has to just integrate them and can concentrate on the core business logic, which is the motto of Spring framework.

MVC Pattern :

There are three components in MVC pattern (basically in any web application), each for a layer.

MVC Components :

  • Model : Model is nothing but an object holding business data, normally speaking it is POJO.
  • View : Once the Model data is returned from the back end, View takes care of rendering it on the browser. View can be implement with UI technologies like HTML, CSS, js frameworks. So the input of View will be model.
  • Controller : If user submits any request, controller takes the request, identifies the type of request and sends it appropriate service method to generate Model Data. So the output of controller will be Model.

Spring Web MVC Framework :

Spring MVC framework is a well designed framework, that you can use to develop web applications using MVC design pattern. There are many benefits to let you move towards Spring framework to implement your web application and it follows same principles as other Spring framework modules.

Benefits Of Spring Web MVC :

  • Since Spring MVC framework is designed like any other Spring module, developer need not spend any extra time to learn it.
  • Since all the layers are independent of each others, unit testing can be easier.
  • Spring framework doesn’t force you to follow any pattern, or implementations to write your business logic. So it gives developer flexibility to implement or integrate any other design pattern to suffice his needs.
  • Spring provides good separation flexibility between Controller, Service and Data access layers.
  • Spring provides you with a tag library that is simple and yet very powerful.
  • View side you can integrate with any UI framework like JSF, Velocity, Freemarker etc.
  • Project setup with Spring is quite easy unlike any other web framework, and it will be light weight since everything is based on POJO.
  • Supports Annotation based programming along with XML, which makes development faster and cleaner.
  • Other built in Sping frameworks like JDBC, Form Validation can be easily integrated.

Spring MVC Architecture flow :

Spring Web MVC Framework Flow

  • Any incoming request that comes to the web application will be sent to Front Controller (Dispatcher Servlet)
  • Front Controller decides to whom (Controller) it has to hand over the request, based on the request headers.
  • Controller that took the request, processes the request, by sending it to suitable service class.
  • After all processing is done, Controller receives the model from the Service or Data Access layer.
  • Controller sends the model to the Front Controller (Dispatcher Servlet).
  • Dispatcher servlet finds the view template, using view resolver and send the model to it.
  • Using View template, model and view page is build and sent back to the Front Controller.
  • Front controller sends the constructed view page to the browser to render it for the user requested.

Spring Web MVC Concepts:

spring web mvc framework concepts

Spring Controller:

Spring provides many types of controllers, developer has to choose one based on his requirement whether you have form in your UI, if you have wizards, etc.
Below is the class diagram showing partial list of controllers.,

Model and View :

ModelAndView is part of org.springframework.web.servlet package. Controller returns the instance of ModelAndView and Model values are placed in object in the form of a map.

Command Object :

This is the object that will be passed once if user submits any form in the front UI, with all the form fields. The same object will be passed to validator to validate the content to check whether it is filled as we need, and it is simple java bean style representation.

Validator :

  • Validator is used to validate the form data received by controller.
  • Custom validator for the form can be created by implementing the interface org.springframework.validation.Validator
  • It has two methods validate and errors, to validate command object passed and errors to store the errors.

Spring Tag Library :

Spring bind type library is very simple and powerful. It is generally used in the jsp pages using <spring:bind> tag, used to bind form fields to command object.

 

Happy Learning 🙂