博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC request生命周期
阅读量:4364 次
发布时间:2019-06-07

本文共 3328 字,大约阅读时间需要 11 分钟。

        When the request leaves the browser, it carries information about what the user is asking for. At very least, the request will be carrying the requested URL. But it may also carry additional data such as the information submitted in a form by the user.

The first stop in the request’s travels 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 DispatcherServlet’s job is to send the request on to a Spring MVC controller. A controller is a Spring component that processes the request. But a typical application may have several controllers and DispatcherServlet needs help deciding which controller to send the request to. So, the DispatcherServlet consults one or more handler mappings C to figure out where the request’s next stop will be. The handler mapping will pay particular attention to the URL carried by the request when making its decision.

        Once an appropriate controller has been chosen, DispatcherServlet sends the request on its merry way to the chosen controller. D At the controller, the request will drop off its payload (the information submitted by the user) and patiently wait for the controller to process that information. (Actually, a well-designed Controller performs little or no processing itself and instead delegates responsibility for the business logic to one or more service objects.)

The logic performed by a controller often results in some information that needs to be carried back to the user and displayed in the browser. This information is referred to as the model. But sending raw information back to the user isn’t sufficient—it needs to be formatted in a user-friendly format, typically HTML. For that the information needs to be given to a view, typically a JSP.

       So, the last thing that the controller will do is package up the model data and the name of a view into a ModelAndView object.  It then sends the request, along with its new ModelAndView parcel, back to the DispatcherServlet. As its name implies, the ModelAndView object contains both the model data as well as a hint to what view should render the results.

       So that the controller isn’t coupled to a particular view, the ModelAndView doesn’t carry a reference to the actual JSP. Instead it only carries a logical name that will be used to look up the actual view that will produce the resulting HTML. Once the ModelAndView is delivered to the DispatcherServlet, the DispatcherServlet asks a view resolver to help find the actual JSP. 

       Now that the DispatcherServlet knows which view will render the results, the request’s job is almost over. Its final stop is at the view implementation (probably a JSP) where it delivers the model data.  With the model data delivered to the view, the request’s job is done. The view will use the model data to render a page that will be carried back to the browser by the (not-so-hard-working) response object.

摘自《 Manning Spring in action》

通过对SpringMVC中request生命周期的理解。我们就很容易记住如何来配置他了。

 

转载于:https://www.cnblogs.com/xinyuyuanm/p/3206683.html

你可能感兴趣的文章
Swift 学习之二十一:?和 !(详解)
查看>>
Laravel
查看>>
二分图匹配
查看>>
Day032--Python--操作系统, process进程
查看>>
highcharts-Highmaps 动态传入城市名称
查看>>
english interview
查看>>
寒假222_codeforces 290 div 2 D
查看>>
open-falcon(v0.2)部署手册(源码编译)
查看>>
大明A+B(hdu1753)大数,java
查看>>
局部变量&&malloc函数&&生命周期的一些见解
查看>>
springboot打jar包,调用webservice出错
查看>>
一审七个月了
查看>>
xth的旅行(codevs 1450)
查看>>
sql 表有没有自增列,插入自增列值
查看>>
php ajax请求判断
查看>>
sqlserver 行列旋转
查看>>
Winform圆角窗体绘制
查看>>
【题解】移动牛棚
查看>>
注解与配置的配合使用
查看>>
查询某个字段属于哪些表
查看>>