Spring 3 MVC Framework Introduction
Spring 3 MVC Framework is a second generation framework which has some advanced features on par with some of the MVC frameworks like Struts 2.0.
Spring MVC framework has undergone a lot of changes from earlier versions which reduces the dependency on the Spring MVC components.
Some of the Important Advantages of Spring 3 MVC are given below:
- Supports RESTful URLS.
- Annotation based configuration due to which there we be reduced XML Based configuration.
- Spring 3 MVC Handler Adapters can be used to customize the work flow of the request and can integrate with other MVC based frameworks like Struts.
- Spring MVC Framework supports almost all kinds of result types like JSP, Velocity, XML, PDF, Tiles etc.,
Model View Controller overview:
MVC Architecture is a very old architecture and yet powerful concept to seperate the responsibilities of the layers to handle the request. The Controller is responsible to receive the request and then invokes some other layers from the model or model can be a POJO into which the controller can put some information for the view. After the controller gets the information from the model, the view will be forwarded and view can used infromation inside the model to display the details to the client.
DispatcherServlet as Front Controller:
Front Controller has a very important role to play in the MVC Frameworks work flow. The front controller component in a MVC framework is responsible to capture the requests landing at the application and route them into the control of the MVC Framework. In Spring 3 MVC the DispatcherServlet acts as the Front Controller and is responsible to caputure the request and route it into the frameworks control.
Apart from capturing the requests, the DispatcherServlet is also responsible to initialize the frameworks components which are used to process the request at various stages.
Request Flow in Spring 3 MVC Framework:
- DispatcherServlet receives the request for a URL in the application.
- The Locale Resolver component will look for the the Locale information in the request header or session or Cookie as the configuration. The Locale is used to pick the resource files based on the language of the user. This Locale Resolver plays a key role in internationalization of the application.
- The Theme Resolver is bound to the request to make the views determine which theme/CSS needs to be applied.
- The Multipart Resolver component is invoked to check if the request is for a file upload and then wraps the request to facilitate the file upload functionality.
- The Handler mapping component is invoked on the request to get the respective controller which is responsible to handle this request.
- The DispatcherServlet then invokes the HandlerChain which will execute the following:
- Checks if there are any interceptors mapped and invokes the Pre Processing logic.
- The controllers handler method will be invoked where the request is processed and the result is returned.
- The mapped interceptors post processing logic will be invoked
- The DispactherServlet based on the result returned by the Controllers handlers method, the ResutlToViewNameTranslator component is invoked to generate the view name.
- The view resolver will then decide on what view needs to be rendered (JSP/XML/PDF/VELOCITY etc.,) and then the result will be dispatched to the client.
DisapatcherServlet Initialization of Framework:
During the application start up, the dispatcher servlet will initialize the following components in the framework:
Spring has built-in multipart support to handle file uploads in web applications. By default, no multipart handling will be done by spring framework. The spring framework will only track for the multipart requests only when there is a component of type MultipartResolver is registered in the spring mvc configuration file.
Spring has a wonderful support for the Internationalization. The DispatcherServlet enables to automatically resolve the locale in the request. This is done using the LocaleResolver component. The different LocaleResolvers implementations are AcceptHeaderLocaleResolver, CookieLocaleResolver, SessionLocaleResovler etc., These components scan the request headers, cookies and session for the locale respectively.
This component is used to apply different CSS based on the request and the current theme.
This component decides on what controllers method needs to be invoked when a request with a HTTP Method arrives. Based on this components response, the DispatcherServlet will invoke the Controllers handler method.
Spring MVC gives support to handle the request to the Actions/Controllers of other MVC Frameworks. This component is not for the application developer. This is implemented by the advanced users who want to have their own flow for the request in the framework.
This component helps to check if there are any exceptions that have occurred during the request processing and if picks the respective view configured for that exception and forwards it to the client.
The RequestToViewNameTranslator helps to transform the URL of the incoming request to a view name.
This component plays a vital role in deciding on the view type that needs to be sent and also prepares the view.
The Spring 3 MVC Framework is a very highly framework which can be tuned to the needs of a MVC application. The most important advantage of using Spring 3 MVC framework is that we can get the dependency Injection and Aspect oriented Programming with this framework. Spring 3 MVC framework is known for its capability to support the Internationalized applications.