Concept Overview
Spring has a well developed and highly plugable framework for Web Development. It is based on Model, View and Controller (MVC) Framework. The DispatcherServlet handles the whole show, right from intercepting the request, to delegating the request to appropriate controllers and rendering the view. It follows the FrontController design pattern. To understand the various classes in the spring MVC framework it would be good to understand how the request flows inside the DispatcherServlet. Here’s a diagram that shows how the DispatcherServlet handles the request.
Here are the steps in detail
- The request is handled by the doRequest method of the DispatcherServlet. It first sets the following attributes – WEB_APPLICATION_CONTEXT_ATTRIBUTE, LOCALE_RESOLVER_ATTRIBUTE, THEME_RESOLVER_ATTRIBUTE, THEME_SOURCE_ATTRIBUTE, INPUT_FLASH_MAP_ATTRIBUTE, OUTPUT_FLASH_MAP_ATTRIBUTE and FLASH_MAP_MANAGER_ATTRIBUTE.
- The request is then converted into a multipart request if a multipart resolver is available.
- The Handler is then obtained for the current request. This is achieved as follows – The code iterates through all handlerMappings and selects the one that satisfies the current request. The code may use the URL, session state or other parameters to determine which handler satisfies the current request. The Handler returned is of type HandlerChainExecution. It contains the interceptors and the Handler . Note that the Handler is of type Object and is not bound to any specific interface. It is therefore possible to use handlers from another frameworks to handle requests. The Default handlerMapping is org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping and org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.(more on HandlerMappings)
- After obtaining the handler, the DispatcherServlet looks for the HandlerAdapter for the current request. Each handler type implements this interface. Each handler type handles the request through the HandlerAdaptor Interface. The default handler adapter for Spring org.springframework.web.HttpRequestHandler is org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter and for org.springframework.web.servlet.mvc.Controller is org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.(more on HandlerAdapter)
- Apply the preHandle method of all interceptors.
- Invoke the handle method using the HandleAdaptor (handleAdaptor.handle(request,response,handler). This returns an object of type ModelAndView.
- If the returned ModelAndView object does not contain a view name or a view object then the configured RequestToViewNameTranslator will translate the current request into a view name. The default implementation of RequestToViewNameTranslator is org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator. Otherwise a ViewResolver is used to resolve the view from the view name
- The view is then rendered.
- Call the triggerAfterCompletion methods of the HandlerInterceptors.
This is the how typically a request travels through the RequestDispatcher, in the end rendering the appropriate view for the user. Spring’s power lies in the fact that at almost each step spring provides configuration opportunity for custom implementation.
Sample Program Overview
The following example shows Spring MVC in action. The example uses SpringMVC to render a JSP that has a list of Objects of Type Persons (name and age)
- commons-beanutils.jar
- commons-collections.jar
- commons-digester.jar
- jstl-1.2.jar
- org.springframework.aop.jar
- org.springframework.asm.jar
- org.springframework.beans.jar
- org.springframework.context.jar
- org.springframework.context.support.jar
- org.springframework.core.jar
- org.springframework.expression.jar
- org.springframework.web.jar
- org.springframework.web.servlet.jar
- retrotranslator-runtime-1.2.3.jar
- servlet-api-2.5.jar
- springmvc.jar
Create the Person class which models a Person with his name and age. This represents the model in MVC pattern.
Create the PersonService class with a method to fetch a list of all the persons (see lines 18-20 below). PersonService class will be used by PersonController (described later) to fetch the list of all persons.
Create the PersonController class. This class is the Controller in MVC pattern and is declared as such by using the
@Controller
annotation (see line 9 below).
When a request URL ends with
/allpersons
then Spring MVC passes the control to
showPersonListPage
method using
@RequestMapping
annotation(see line 23 below).
PersonController class delegates the call to
PersonService
class to fetch all the persons and binds it to the model name
persons
(see line 25 below) as a request attribute. This model name will be used in the view JSP (described later).
PersonController.showPersonListPage() method returns
person_list
which is the name of the view JSP.
Create person_list.jsp as shown below. This represents the view in MVC pattern.
Get the list of persons by using the name of the model
persons
(see line 17 below). Note that this model was set in PersonController.showPersonListPage() method with the same model name.
Iterate the list persons and display the list in a table (see lines 18-23 below).
Create the web.xml configuration as shown below.
Create a servlet mapping with name
person
(see lines 13-16 below) and define the corresponding servlet using Spring
DispatcherServlet
class (see lines 7-11 below).
Note that Spring automatically will look for a configuration file called
person-servlet.xml
corresponding to the servlet mapping name person. We shall describe the details of
person-servlet.xml
later.
Create the Spring configuration by the name
person-servlet.xml
as shown below.
Configure Spring such that annotation based wiring will be used (see lines 14-15 below).
Configure Spring such that the prefix
/views
and the suffix
.jsp
should be added to the name of the view JSP as specified in PersonController.showPersonListPage() method (see lines 18-26 below)
Finally, we need to deploy this web program in a web container and access it using a browser to verify that it executes correctly.
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. As this sample program contains Java Server Pages (JSPs), you will need Java Development Kit (JDK preferably 1.5 or higher) on your machine so that the JSPs can be complied locally. Note that no other setup is required on your machine! Also please ensure that the port 8080 is not being used by any other program on your machine.
(Alternatively you can go the folder containing the springmvc-installer.jar and execute the jar using
java -jar springmvc-installer.jar
command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
springmvc
. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.