通过前面的例子,我们实现了spring mvc模式,那么在spring mvc模式下请求是如何被执行最终返回页面的呢?
从服务器收到请求开始,执行流程如下:
1.服务器接收请求,根据web.xml的配置找到响应的Servlet,使用 spring mvc 我们会将所有的请求交给DispatcherServlet。
2.DispatcherServlet会在初始化时从配置文件中获取所有HandlerMapping对象,通过遍历HandlerMapping找到处理请求对应的Controller。
3.调用Controller对象的响应方法,返回ModelAndView结果。
3.对ModelAndView进行处理渲染出页面。
4.返回处理页面结果。
其实DispatcherServlet的实际过程要远比上面的流程复杂的多,但是为了便于理解,我们先这么简化流程,在后续的章节中会进一步讲解详细的细节。