[JAVA] Fonctionnement du servlet Dispatcher dans Spring MVC

Role of Spring MVC in Dispatcherservlet

As I said, DispatcherServlet wears many hats in Spring. It acts as a front controller and provides a single entry point for the application. It then uses handler mappings and handler adapters to map a request to the Spring MVC controllers. It uses @Controller and @RequestMapping annotation for that purpose.    Once the request is processed by the Spring MVC controller, it returns a logical view name instead of the view. Though, you can even configure Controler's handler methods to not return any View name by declaring return type as void. You can even use @ResponseBody annotation in the case of REST to directly write the output to the HTTP response body. See REST with Spring course by Eugen to learn more about developing RESTful web services using Spring MVC.    When DispatherServlet receives view name, it consults the ViewResolver to find the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They try to resolve the logical view name into a Physical resource e.g. a JSP page or a FreeMaker or Velocity template.    The ViewResolver are invoked in order, if first in the chain not able to resolve the view then it returns null and next ViewResolver in the chain is consults. Once the right view is found, DispatcherServlet forwards the request along with Model data to the View for rendering e.g. a JSP page.      By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix and suffix to convert a logical view name e.g. "home" to /WEB-INF/home.jsp. The View interface also has getContentType() method, which returns content type the view produces (JstlView has text/html). This is usually the default content type for requests handled by the dispatcher servlet in Spring.    Here is a nice diagram which explains how DispatcherServlet works internally in Spring MVC In short, DispatcherServlet is used following things in Spring MVC  - receives all request as Front Controller and provides a single entry point to the application  - mapping requests to correct Spring MVC controller  - Consulting ViewResolvers to find correct View  - forwarding request to chosen View for rendering  - returning the response to the client  - creates web-context to initialize the web specific beans e.g. controllers, view resolvers and handler mapping    That's all about what is the use of DispatcherServlet in Spring framework. It's is one of the key components of Spring MVC which is used to receive all incoming request and forward them to right controllers for actual processing. It finds the right controllers by using handler mappings e.g. SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which check if the bean name is the same as view name and the bean implements the View interface.    If you are using annotations then it can also use @Controller and @RequestMapping annotations to find the right controller to process a particular request. Once the request is processed by the controller it returns a logical view name to DispatcherServlet.    The DispatcherServlet then consults ViewResolver and LocalResolvers to find the right View to render the output. Once the correct View is chosen, it forwards the request to the View for rendering the response.

Related blog:

Java concurrency method example

Recommended Posts

Fonctionnement du servlet Dispatcher dans Spring MVC
[Java] Fonctionnement de Spring DI
Comment utiliser Lombok au printemps
Comment fonctionne la mémoire dans les langages orientés objet
Comment inclure Spring Tool dans Eclipse 4.6.3?
Comment Spring Security fonctionne avec Hello World
[Spring MVC] Comment transmettre des variables de chemin
Cookie SameSite dans Spring Boot (Spring Web MVC + Tomcat)
Testez le contrôleur avec Mock MVC dans Spring Boot
Découvrez comment HashMap fonctionne en Java
Utiliser le filtre de servlet avec Spring Boot [compatible Spring Boot 1.x, 2.x]
MVC dans Eclipse.
Comment se lier avec un fichier de propriétés dans Spring Boot
Comment définir plusieurs orm.xml dans Spring4, JPA2.1
Comment fonctionne jul-to-slf4j
Comment créer un projet Spring Boot dans IntelliJ
Comment utiliser CommandLineRunner dans Spring Batch of Spring Boot
Comment tester l'écran de téléchargement de fichiers avec Spring + Selenium
Comment utiliser le référentiel de jobs en mémoire avec Spring Batch
Inject Logger au printemps
Mappage du servlet Spring Boot
Utilisez Interceptor au printemps
Microservices dans Spring Cloud
Obtenez des cookies au printemps
Comment modifier la valeur de paramètre de application.properties au moment du démarrage dans Spring Boot
Comment appeler et utiliser l'API en Java (Spring Boot)
Activer les WebJars pour les projets vierges dans TERASOLUNA 5.x (= Spring MVC)
Connectez-vous avec HttpServletRequest # login dans Spring Security dans l'environnement Servlet 3.x
Comment contrôler les transactions dans Spring Boot sans utiliser @Transactional