Java Reference
In-Depth Information
The
HandlerMapping
interface is the key to the overall action handler component. On
intercepting a request, the dispatcher servlet looks for the appropriate handler mapping
object to map between the request and the request-processing object. In other words, a
handler mapping provides an abstract way to map the request URL to the eventual han-
dlers or page controllers.
As the name suggests,
AbstractHandlerMapping
is an abstract handler mapping imple-
mentation. It implements the
getHandler
method of the
HandlerMapping
interface. This
method returns a
HandlerExecutionChain
, which holds references to the following:
• Single page controller implementations that implement either the
Controller
interface or the
ThrowawayController
interface
• An optional set of interceptors implementing the
HandlerInterceptor
interface
The
BeanNameUrlHandlerMapping
and
SimpleUrlHanderMapping
provide two concrete
implementations of the
HandlerMapping
interface that are sufficient for most cases. The
BeanNameUrlHandlerMapping
is the default handler mapping used by the front controller. If
your application needs only this handler mapping, then there is nothing to configure. In
some cases, your application may require multiple handler mappings. As I will show in a
later section, Spring MVC allows multiple handler mappings to work side by side. In this
case, the front controller has to decide the order in which the handler mappings will be
invoked. The handler mappings implement the
Ordered
interface, allowing the front con-
troller to decide the right ordering in the handler mapping chain. The handler mapping
with lowest order value has the highest priority.
A handler mapping holds only the reference of a page controller. It does not invoke
any method on it. A handler adapter is responsible for invoking methods on a page
controller. All handler adapters implement the
HandlerAdapter
interface. As the name
suggests, handler adapters follow the GOF adapter design pattern and have the best
knowledge to invoke the appropriate page controller. This opens up another Spring
extension point and allows easy integration of page controllers from other frameworks
such as WebWork or Struts
Action
classes. The
handle
method should be implemented to
invoke methods on the page controller. This is exactly what the concrete implementation
class
SimpleControllerHandlerAdapter
does. It is capable of invoking page controllers
that implement the
Controller
interface. In other words, it knows how to invoke the
handleRequest
method and handle the returned value.
The
HandlerExecutionChain
also contains an optional set of interceptors. These pro-
vide a robust mechanism for declarative processing before and after a request is handled
by the page controller. The sequence diagram in Figure 3-4 is an extension of Figure 3-1
and shows the complete message exchange within the action handler part of the Spring
MVC application controller component.
