Friday 23 March 2012

Spring's DispatcherServlet

Spring's DispatcherServlet do more than just dispatches requests to controllers. It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.



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.

code eg:

<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>*.html</url-pattern>
    </servlet-mapping>

</web-app>
 
In a application there can be more than one DispatcherServlet defined 
and each DispatcherServlet have its own WebApplicationContest.

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).
 
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
and that it knows which servlet it is associated with. The WebApplicationContext
is bound in the ServletContext.
 
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. 

1.  Controllers
2. Handler mappings
3. View resolvers
4. Locale resolver
5. Theme resolver
6. multipart file resolver
7. Handler exception resolver(s) 

No comments:

Post a Comment