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.
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.
相关文章推荐
- Spring MVC How take the parameter value of a GET HTTP Request in my controller method?
- How does the Spring MVC determine the type of a given request parameter
- An overview of the Spring MVC request flow
- A Day in the Life of a Web Page Request
- The flow of MVC in Spring -- Part I -- Dispatcher and Controller
- Spring in Action 3 - the startup lifecycle of a typical bean
- An error message detailing the cause of this specific request failure can be found in the application event log of the
- Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法
- springmvc No mapping found for HTTP request with URI in Dispatc
- Configure the handler mapping priority in Spring MVC
- No mapping found for HTTP request with URI [/springmvc/queryitems.action] in DispatcherServlet with
- IDEA - MySql / Spring mvc : Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryErro
- Spring MVC @ResponseBody 提示 406 The resource identified by this request……
- springmvc No mapping found for HTTP request with URI in Dispatc
- Spring MVC "The request sent by the client was syntactically incorrect "解决办法
- Spring MVC "The request sent by the client was syntactically incorrect "解决办法
- 异常处理Spring MVC "The request sent by the client was syntactically incorrect ()"
- The Importance of Money in Life
- Configure the handler mapping priority in Spring MVC
- Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法