What is Dispatcher Servlet in Spring?

前端 未结 7 1602
一整个雨季
一整个雨季 2020-11-30 15:56

In this image (which I got from here), HTTP request sends something to Dispatcher Servlet.

相关标签:
7条回答
  • 2020-11-30 16:53

    We can say like DispatcherServlet taking care of everything in Spring MVC.

    At web container start up:

    1. DispatcherServlet will be loaded and initialized by calling init() method
    2. init() of DispatcherServlet will try to identify the Spring Configuration Document with naming conventions like "servlet_name-servlet.xml" then all beans can be identified.

    Example:

    public class DispatcherServlet extends HttpServlet {
    
        ApplicationContext ctx = null;
    
        public void init(ServletConfig cfg){
            // 1. try to get the spring configuration document with default naming conventions
            String xml = "servlet_name" + "-servlet.xml";
    
            //if it was found then creates the ApplicationContext object
            ctx = new XmlWebApplicationContext(xml);
        }
        ...
    }
    

    So, in generally DispatcherServlet capture request URI and hand over to HandlerMapping. HandlerMapping search mapping bean with method of controller, where controller returning logical name(view). Then this logical name is send to DispatcherServlet by HandlerMapping. Then DispatcherServlet tell ViewResolver to give full location of view by appending prefix and suffix, then DispatcherServlet give view to the client.

    0 讨论(0)
提交回复
热议问题