Overview about request processing in Spring MVC

In the tutorial about the MVC Model, you guys know that for the web environment, we need a Front Controller to receive the request and return a response. DispatcherServlet is a Front Controller like that in Spring MVC. In this tutorial, we will look at the overview of the Spring DispatcherServlet and request processing process in Spring MVC.

First, I will create a Spring MVC project as an example:

Overview about request processing in Spring MVC

For an overview about the Spring MVC request processing process, let’s do a test.

In the project above, in the /src/main/resources directory, you see a configuration file for Log4J, log4j.xml. The contents of this file are as follows:

Just change the level of loggers for the Spring framework from info to debug in the “3rdparty Loggers” section and the root logger in the priority section from warn to debug. After modification, the contents of the log4j.xml file will look like this:

Now, let’s try running our application. You can refer to how to run Spring MVC application here. I configured the Maven Jetty Plugin as follows:

Result:

Overview about request processing in Spring MVC

Now look at the console in our STS IDE, and you’ll see the following logs:

Obviously, dissection of logs above, you can see, DispatcherServlet will be the first class to receive the request, then it uses the RequestMappingHandlerMapping class to find the appropriate controller to handle our request.

After the HomeController has finished processing the request and returns the result, the DispatcherServlet will find the corresponding view object returned by the HomeController, which is JstlView.

At the end of processing a request, DispatcherServlet will print a message “Successfully completed request”.

For an overview, the process of processing a request in Spring MVC can be summarized using the following diagram:

Overview about request processing in Spring MVC

  1. Request from the user to our web application, DispatcherServlet will be the object receiving the request.
  2. DispatcherServlet will find and navigate the request to the appropriate handler, here are the controllers in our web application.
  3. Processing the request
  4. Prepare the model and select the display view.
  5. Returns the request processing result to the DispatcherServlet.
  6. The DispatcherServlet will invoke the appropriate View Template to handle the display on the view using the model.
  7. View Template returns results to DispatcherServlet.
  8. Returns the response to the user.

 

Add Comment