1. Introduction
1.1 Spring Framework
- Spring is an open-source framework created to address the complexity of an enterprise application development
- One
of the chief advantages of the Spring framework is its layered
architecture, which allows developers to be selective about which of its
components they can use while providing a cohesive framework for
J2EE application development
- Spring framework provides support and integration to various technologies for e.g.:
- Support for Transaction Management
- Support for interaction with the different databases
- Integration with the Object Relationship frameworks for e.g. Hibernate, iBatis etc
- Support for Dependency Injection which means all the required dependencies will be resolved with the help of containers
- Support for
REST style web-services
1.2 Spring MVC Framework
Model-View-Controller (MVC)
is a well-known design pattern for designing the GUI based
applications. It mainly decouples the business logic from UI by
separating the roles of Model, View, and Controller
in an application. This pattern divides the application into three
components to separate the internal representation of the information
from the way it is being presented to the user. The three components
are:
- Model (M): Model’s responsibility is to manage the application’s data, business logic, and the business rules. It is a
POJO class which encapsulates the application data given by the controller
- View (V):
A view is an output representation of the information, such as
displaying information or reports to the user either as a text-form or
as charts. Views are usually the
JSP templates written with Java Standard Tag Library (JSTL)
- Controller(C):
Controller’s responsibility is to invoke the Models to perform the
business logic and then update the view based on the model’s output. In
Spring framework, the controller part is played by the Dispatcher
Servlet
1.2.1 Spring MVC Architecture and Flow
The main component of Spring MVC framework is the Dispatcher Servlet. Refer to the below diagram to understand the Spring MVC architecture.
In Spring 3 MVC framework Dispatcher Servlet
access the front controller which handles all the incoming requests and
queues them for forwarding to the different controllers.
- Dispatcher Servlet is configured in the
web.xml
of the application and all the requests mapped to this servlet will be
handled by this servlet. Dispatcher Servlet delegates the request to the
controller (i.e. class annotated with the @Controller annotation)
- The Controller class invokes the appropriate handler method based on the
@RequestMapping annotation. This method returns the logical name of the View and the Model
- Dispatcher Servlets resolves the actual view name using the
ViewResolver (configured in the Spring Beans configuration file) and gets the actual view name
- Passes the model object to the view so it can be used by view to display the result to the user
1.2.2 Advantages of Spring MVC Framework
- Supports RESTful URLs
- Annotation based configuration (i.e. developers may reduce the metadata file or less of configuration)
- Supports to plug with other MVC frameworks like
Struts, Struts2, JSF etc
- Flexible in supporting different View types like
JSP, Velocity, XML, PDF, Tiles etc