您的位置:首页 > 编程语言 > Java开发

The Life of a request in Spring MVC

2006-11-01 00:20 399 查看
How Spring MVC working? That's a question. Now, let's see the life of a request in Spring MVC, and when we finish it, we will know how it works.
Let's Begin:
Figure 1 shows the life cycle of a request from start to finish.



Figure 1
The process starts when a client sends a request. The first component to receive the request is Spring's DispatcherServlet. Like most Java-based MVC frameworks, Spring MVC funnels requests through a single front controller servlet. A front controller is a common web-application pattern where a single servlet delegates responsibility for a request to other components of an application to perform the actual processing. In the case of Spring MVC, DispatcherServlet is the front controller.
The Spring MVC component that is responsible for handling the request is a Controller. To figure out which controller should handle the request, DispatcherServlet starts by querying one or more HandlerMappings. A HandlerMapping typically performs its job by mapping URL patterns to Controller objects.
Once the DispatcherServlet has a Controller object, it dispatches the request to the Controller to perform whatever business logic it was designed to do.
Upon completion of business logic, the Controller returns a ModelAndView object to the DispatcherServlet. The ModelAndView can either contain a View object or a logical name of a View object.
If the ModelAndView object contains the logical name of a View, the DispatcherServlet queries a ViewResolver to look up the View object that will render the response. Finally, the DispatcherServlet dispatches the request to the View object indicated by the ModelAndView object. The View object is responsible for rendering a response back to the client.
This is the life of the Request from the client in Spring MVC. DispatcherServlet here like a front controller, in fact, it is, and everything has been done by it. So, the DispatcherServlet is the heart of Spring MVC. We should load it when the server start up. Like any servlet, DispatcherServlet must be configured in web application's web.xml file.
<servlet>
<servlet-name>name</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
And then, you can indicate what URLs will be handled by the DispatcherServlet. Add the following <servlet-mapping> to web.xml to let DispatcherServlet handle all URLs that end in any suffix.
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>*.suffix</url-pattern>
<servlet-mapping>
That's it. You finish the front controller, DispatcherServlet, to map any *.suffix request.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐