Spring's web MVC framework is, like many other web MVC frameworks,
request-driven, designed around a central servlet that dispatches requests
to controllers and offers other functionality facilitating the development
of web applications. Spring's DispatcherServlet
however, does more than just that. It is completely integrated with the
Spring IoC container and as such allows you to use every other feature
that Spring has.
The request processing workflow of the Spring Web MVC
DispatcherServlet
is illustrated in the following
diagram. The pattern-savvy reader will recognize that the
DispatcherServlet
is an expression of the
“Front Controller” design pattern (this is a pattern that
Spring Web MVC shares with many other leading web frameworks).
The requesting processing workflow in Spring Web MVC (high level)
The DispatcherServlet
is
an actual Servlet
(it inherits from the
HttpServlet
base class), and as such is declared in
the web.xml
of your web application. Requests that you
want the DispatcherServlet
to handle will have to
be mapped using a URL mapping in the same web.xml
file.
This is standard J2EE servlet configuration; an example of such a
DispatcherServlet
declaration and mapping can be
found below.
<web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping> </web-app>
In the example above, all requests ending with
.form
will be handled by the
'example'
DispatcherServlet
.
This is only the first step in setting up Spring Web MVC... the various
beans used by the Spring Web MVC framework (over and above the
DispatcherServlet
itself) now need to be
configured.
As detailed in the section entitled Section 4.8, “The ApplicationContext”,
ApplicationContext
instances in Spring can
be scoped. In the web MVC framework, each
DispatcherServlet
has its own
WebApplicationContext
, which inherits all
the beans already defined in the root
WebApplicationContext
. These inherited
beans defined can be overridden in the servlet-specific scope, and new
scope-specific beans can be defined local to a given servlet
instance.
Context hierarchy in Spring Web MVC
The framework will, on initialization of a
DispatcherServlet
, look for a file named
[servlet-name]-servlet.xml
in the
WEB-INF
directory of your web application and create
the beans defined there (overriding the definitions of any beans defined
with the same name in the global scope).
Consider the following DispatcherServlet
servlet configuration (in the 'web.xml'
file.)
<web-app> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
With the above servlet configuration in place, you will need to have
a file called '/WEB-INF/golfing-servlet.xml'
in your application;
this file will contain all of your Spring Web
MVC-specific components (beans). The exact location of this
configuration file can be changed via a servlet initialization parameter
(see below for details).
The WebApplicationContext
is an
extension of the plain ApplicationContext
that has some extra features necessary for web applications. It differs
from a normal ApplicationContext
in that it
is capable of resolving themes (see Section 16.7, “Using themes”),
and that it knows which servlet it is associated with (by having a link to
the ServletContext
). The
WebApplicationContext
is bound in the
ServletContext
, and by using static methods
on the RequestContextUtils
class you can always
lookup the WebApplicationContext
in case
you need access to it.
The Spring DispatcherServlet
has a couple of
special beans it uses in order to be able to process requests and render
the appropriate views. These beans are included in the Spring framework
and can be configured in the
WebApplicationContext
, just as any other
bean would be configured. Each of those beans is described in more detail
below. Right now, we'll just mention them, just to let you know they exist
and to enable us to go on talking about the
DispatcherServlet
. For most of the beans, sensible
defaults are provided so you don't (initially) have to worry about
configuring them.
Table 16.1. Special beans in the
WebApplicationContext
Bean type | Explanation |
---|---|
Controllers | Controllers are the
components that form the 'C' part of the
MVC. |
Handler mappings | Handler mappings handle the execution of a list of pre- and post-processors and controllers that will be executed if they match certain criteria (for instance a matching URL specified with the controller) |
View resolvers | View resolvers are components capable of resolving view names to views |
Locale resolver | A locale resolver is a component capable of resolving the locale a client is using, in order to be able to offer internationalized views |
Theme resolver | A theme resolver is capable of resolving themes your web application can use, for example, to offer personalized layouts |
multipart file resolver | A multipart file resolver offers the functionality to process file uploads from HTML forms |
Handler exception resolver(s) | Handler exception resolvers offer functionality to map exceptions to views or implement other more complex exception handling code |
When a DispatcherServlet
is set up for use
and a request comes in for that specific
DispatcherServlet
, said
DispatcherServlet
starts processing the request.
The list below describes the complete process a request goes through when
handled by a DispatcherServlet
:
The WebApplicationContext
is
searched for and bound in the request as an attribute in order for the
controller and other elements in the process to use. It is bound by
default under the key
DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE
.
The locale resolver is bound to the request to let elements in the process resolve the locale to use when processing the request (rendering the view, preparing data, etc.) If you don't use the resolver, it won't affect anything, so if you don't need locale resolving, you don't have to use it.
The theme resolver is bound to the request to let elements such as views determine which theme to use. The theme resolver does not affect anything if you don't use it, so if you don't need themes you can just ignore it.
If a multipart resolver is specified, the request is inspected
for multiparts; if multiparts are found, the request is wrapped in a
MultipartHttpServletRequest
for further
processing by other elements in the process. (See the section entitled
Section 16.8.2, “Using the
MultipartResolver” for further information
about multipart handling).
An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) will be executed in order to prepare a model (for rendering).
If a model is returned, the view is rendered. If no model is returned (which could be due to a pre- or postprocessor intercepting the request, for example, for security reasons), no view is rendered, since the request could already have been fulfilled.
Exceptions that are thrown during processing of the request get
picked up by any of the handler exception resolvers that are declared in
the WebApplicationContext
. Using these
exception resolvers allows you to define custom behaviors in case such
exceptions get thrown.
The Spring DispatcherServlet
also has support
for returning the last-modification-date, as
specified by the Servlet API. The process of determining the last
modification date for a specific request is straightforward: the
DispatcherServlet
will first lookup an appropriate
handler mapping and test if the handler that is found implements
the interface LastModified
interface. If so, the value of the long
getLastModified(request)
method of the
LastModified
interface is returned to the
client.
You can customize Spring's DispatcherServlet
by adding context parameters in the web.xml
file or
servlet initialization parameters. The possibilities are listed
below.
Table 16.2. DispatcherServlet
initialization
parameters
Parameter | Explanation |
---|---|
contextClass | Class that implements
WebApplicationContext , which will
be used to instantiate the context used by this servlet. If this
parameter isn't specified, the
XmlWebApplicationContext will be
used. |
contextConfigLocation | String which is passed to the context instance (specified
by contextClass ) to indicate where context(s)
can be found. The string is potentially split up into multiple
strings (using a comma as a delimiter) to support multiple
contexts (in case of multiple context locations, of beans that are
defined twice, the latest takes precedence). |
namespace | the namespace of the
WebApplicationContext . Defaults to
[servlet-name]-servlet . |