Home >Java >javaTutorial >Execution steps after the Spring MVC project is started (with schematic diagram)
The content of this article is about the execution steps after the Spring MVC project is started (with schematic diagram). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Execution process after the Spring MVC project is started:
1. Execution process:
01. When the web project is started, load the web.xml file, including the core configuration file
Because the core controller of mvc is set in the web.xml file, this controller will be initialized
02. For example, the user's request path is localhost:8080/hello
At this time, /hello will be regarded as the request path ====》The id of the bean in the core configuration file
03./hello will be intercepted by the core controller and go to the processor mapper (HandlerMapping)
04. The bottom layer first finds the appropriate HandlerMapping and then returns a processor execution chain (HandlerExecutionChain)
05. The processor execution chain contains a controller that is about to be executed and (1-N Interceptor)
06. Find HandlerAdapter according to the handler of the processor execution chain
07.HandlerAdapter executes the specified method according to the type of our controller
08.After executing the method Need to return a ModleAndView
Modle ==》Map
View ==》String
09. If the view parser is set, it will be executed. The view is actually a logic View name
If the view parser is not set, it will be executed. The prefix and suffix have no value. The view must be a physical view name
Schematic diagram:
2. View the underlying code
01. Click on the core controller in the web.xml file
02. Ctrl o Find the corresponding method doDispatch
03. Start analyzing the code
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) HttpServletRequest processedRequest = request; //请求对象 HandlerExecutionChain mappedHandler = null; //处理器执行链=我们即将执行的Controller+(1+拦截器) boolean multipartRequestParsed = false; //解析一个请求是否是文件上传请求
04. Find the following code
// Determine handler for the current request. mappedHandler = getHandler(processedRequest);
Find the Handler (Controller) that needs to be executed according to our request
Ctrl Click the left mouse button into getHandler()
I found that the return value of this method is a processor execution chain!
05. After entering getHandler, find the following code
HandlerExecutionChain handler = hm.getHandler(request);
06. Continue to enter hm.getHandler(request)
After entering, you will find that this method is an interface ( There is no method to implement the method in HandlerMapping), so continue to click on the implementation class AbstractHandlerMapping
07. Find the following code and break the point
Object handler = getHandlerInternal(request);
08. Afterwards, you find that the obtained processor is executed The chain gives our core controller
// Determine handler adapter for the current request. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
Click to enter getHandlerAdapter()
09. Find the code break point below
if (ha.supports(handler)) { return ha; }
10. Click to enter supports()
11. Found that supports()
is in the interface, but there are 5 implementation classes. Springmvc will execute 3 by default.
But the HelloController we wrote is implemented indirectly. Controller interface, so we only need to break the point in SimpleControllerHandlerAdapter!
12. Return the obtained HandlerAdapter to the core controller
13. With the processor adapter in the core controller, the next step is to execute the real code
// Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
14. Click to enter handle
@Override public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return ((Controller) handler).handleRequest(request, response); }
15. Click to enter handleRequest
to enter the AbstractController. Our HelloController inherits this class!
This parent class executes handleRequestInternal(request, response);
16. Click on handleRequestInternal
and you will find our implementation class
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("您已经进入了后台的controller/action/servlet"); return new ModelAndView("/WEB-INF/welcome.jsp"); }
The above is the detailed content of Execution steps after the Spring MVC project is started (with schematic diagram). For more information, please follow other related articles on the PHP Chinese website!