Expert Spring MVC and Web Flow

(Dana P.) #1
With the root application configured, the last piece of this puzzle is now required. The
main servlet for the application, which provides the central entry point for the application,
needs to be specified. The org.springframework.web.servlet.DispatcherServletplays this
role, coordinating the processing pipeline for each incoming web request.

DispatcherServlet
The DispatcherServletis the Front Controller of the system, handling all incoming
requests and coordinating the different subsystems of Spring MVC. For instance, the
DispatcherServletshuttles the ModelAndViewreturned by Controllers to the appropriate
ViewResolvers. These, and many other, subsystems are combined to create a sophisticated
and flexible processing pipeline, with each step abstracted for easy customization.
The DispatcherServletalso creates the WebApplicationContext, which contains the web-
specific components such as the Controllers and ViewResolver. The WebApplicationContextis
then nested inside the root ApplicationContextso that the web components can easily find
their dependencies.

■Tip For more on ApplicationContextnesting, please consult the book Pro Spring.


To locate the XML for the WebApplicationContext, the DispatcherServletwill by default
take the name of its servlet definition from web.xml, append -servlet.xml, and look for that
file in /WEB-INF. For instance, if the servlet is named spring, it will look for a file named
/WEB-INF/spring-servlet.xml. Of course, as with nearly everything in Spring MVC, the
location of the WebApplicationContextconfiguration file is itself configurable.
Let’s add the DispatcherServletto web.xmlnow (see Listing 4-13).

Listing 4-13.Adding the DispatcherServlet to web.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>

As you can see, we also took this opportunity to define the URL mapping for the servlet.

60 CHAPTER 4 ■JUMP INTO SPRING MVC

Free download pdf