SpringMVC request生命周期


When the request leaves the browser, it carries information about what theuser is asking for. At very least, the request will be carrying the requested URL. Butit 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 . Likemost Java-based MVC frameworks, Spring MVC funnels requests through a singlefront controller servlet. A front controller is a common web-application patternwhere a single servlet delegates responsibility for a request to other componentsof 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 typicalapplication may have several controllers and DispatcherServlet needs helpdeciding 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 therequest on its merry way to the chosen controller. D At the controller, the requestwill drop off its payload (the information submitted by the user) and patiently waitfor the controller to process that information. (Actually, a well-designed Controllerperforms little or no processing itself and instead delegates responsibility for thebusiness logic to one or more service objects.)

The logic performed by a controller often results in some information thatneeds to be carried back to the user and displayed in the browser. This informationis referred to as the model. But sending raw information back to the user isn’tsufficient—it needs to be formatted in a user-friendly format, typically HTML. Forthat 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 thename of a view into a ModelAndView object. It then sends the request, along withits 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 viewshould render the results.

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

Now that the DispatcherServlet knows which view will render the results, therequest’s job is almost over. Its final stop is at the view implementation (probably aJSP) 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 willbe carried back to the browser by the (not-so-hard-working) response object.


摘自《 Manning Spring in action》


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值