If you look at the Java EE FrontController sequence diagram, the Controller delegates the request to Dispatcher and the document says that:
A dispatcher is responsible for view management and navigation, managing the choice of the next view to present to the user, and providing the mechanism for vectoring control to this resource.
In Spring MVC, the DispatcherServlet
acts as FrontController (as mentioned in Book by Craig Walls), and this Servlet delegates the request to other controllers that in turn calls appropriate Service class (for processing the request) and then returns an instance of ModelAndView
to DispatcherServlet
again.
So roughly this is how request usually travels:
Client -> DispatcherServlet -> Controller -> Service -> DAO
If you compare this flow with Java EE FrontController pattern sequence diagram, it appears that DispatcherServlet
is not true FrontController.
What do you say about this?
The DispatcherServlet is the front controller in Spring web applications. It's used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.
There are three main parts to this: setting the prefix, which sets the default URL path to find the set views within. the default view type which is set via the suffix. setting a view class on the resolver which allows technologies like JSTL or Tiles to be associated with the rendered views.
A Front Controller is nothing but a controller that handles all requests for a website. They are often used in Web applications to implement workflows. 4) The DispatcherServlet is an actual Servlet, it inherits from the HttpServlet base class.
In Spring MVC all incoming requests go through a single servlet is called Dispatcher Servlet (front controller). The front controller is a design pattern in web application development. A single servlet receives all the request and transfers them to all other components of the application.
I would say that the DispatcherServlet fills the rolls of the front-controller and dispatcher. However, rather than delegating directly to the view the DispatcherServlet delegates to another controller. This enables you to better separate your presentation from your business logic. In the "pure" front-controller paradigm, you might have to add some business logic to your views.
In short, the DispatcherServlet accomplishes the same goals as the Front Controller pattern. But it does deviate from it slightly by allowing you to add another layer of controllers to the dispatcher. I think this is a good thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With