tags:

views:

227

answers:

2

http://maestric.com/wiki/lib/exe/fetch.php?w=&h=&cache=cache&media=java:spring:spring_mvc.png

In the above image, HTTP request sends something to Dispatcher Servlet. My Question is what does Dispatcher Servlet do.

Is it something like getting the information thrown from the web page and throwing it to the controller.

+5  A: 

DispatcherServlet is Spring MVC's implementation of the front controller pattern.

See description in the Spring docs here.

Essentially, it's a servlet that takes the incoming request, and delegates processing of that request to one of a number of handlers, the mapping of which is specific in the DispatcherServlet configuration.

skaffman
Is it something like events in Flex, where i get dispatch events from a one MXML to another or to server. Can i have more than one DispatcherServlet in my appication.Do each class files have a separate DispatcherServlet.
There's usually only one front controller. This is regardless of the models and views you have. It just brings specific models and views together.
BalusC
@theband: You *can* have multiple `DispatcherServlets`, if your architecture makes more sense that way, but usually there's no reason to.
skaffman
+3  A: 

The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location.

I might have a file /WEB-INF/jsp/pages/Home.jsp

and a method on a class

@RequestMapping(value="/pages/Home.html")
private ModelMap buildHome() {
return somestuff;
}

The dispatcher servlet is the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document.

How it accomplishes this varies widely with configuration and Spring version.

There's also no reason the end result has to be web pages. It can do the same thing to locate RMI end points, handle SOAP requests, anything that can come into a servlet.

Affe
Great riposte, now a question how come the DispatcherServlet identifies the class name and method name too. Can you show me an example of a configuration where i have two classes and two method names and how DispatcherServlet catches the right request.
It actually scans the class path on start up for that annotation and makes a mapping of the "/pages/Home.html" to the Class + Method. If you had two methods that both had "/pages/Home.html" with no other restrictions in their annotation, that would be an error and it will toss exceptions at you.You can also wire it together with XML if you're oldschool.
Affe