The Life of a request in Spring MVC

 

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.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值