[JAVA] Funktionsweise des Dispatcher-Servlets in 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

Funktionsweise des Dispatcher-Servlets in Spring MVC
[Java] Wie Spring DI funktioniert
Wie man Lombok im Frühling benutzt
Wie Speicher in objektorientierten Sprachen funktioniert
Wie kann ich Spring Tool in Eclipse 4.6.3 einbinden?
Wie Spring Security mit Hello World funktioniert
[Spring MVC] Übergeben von Pfadvariablen
SameSite-Cookie im Spring Boot (Spring Web MVC + Tomcat)
Testen Sie den Controller mit Mock MVC im Spring Boot
Tauchen Sie ein in die Funktionsweise von HashMap in Java
Servlet-Filter mit Spring Boot verwenden [Spring Boot 1.x, 2.x kompatibel]
MVC in Eclipse.
So binden Sie mit einer Eigenschaftendatei in Spring Boot
So definieren Sie mehrere orm.xml in Spring4, JPA2.1
Wie jul-to-slf4j funktioniert
So erstellen Sie ein Spring Boot-Projekt in IntelliJ
Verwendung von CommandLineRunner im Spring Batch von Spring Boot
So testen Sie den Bildschirm zum Hochladen von Dateien mit Spring + Selenium
Verwendung des In-Memory-Job-Repositorys mit Spring Batch
Injizieren Sie den Logger im Frühjahr
Spring Boot Servlet-Zuordnung
Verwenden Sie Interceptor im Frühjahr
Microservices in Spring Cloud
Holen Sie sich Cookies im Frühling
So ändern Sie den Einstellungswert von application.properties beim Booten im Frühjahrsstart
Aufrufen und Verwenden der API in Java (Spring Boot)
Aktivieren Sie WebJars für leere Projekte in TERASOLUNA 5.x (= Spring MVC).
Melden Sie sich mit HttpServletRequest # in Spring Security in der Servlet 3.x-Umgebung an
So steuern Sie Transaktionen in Spring Boot ohne Verwendung von @Transactional