Spring Mvc Flow
By AmarSivas | | Updated : 2021-03-15 | Viewed : 8891 times

The current lesion will give an idea about Spring Web MVC control flow. It is very important to know about control flow/ Request Processing to understand about Spring Web MVC. And we will learn about the Dispatcher servlet configuration as well.
Table of Contents:
Spring Web MVC Control/Request Processing Flow
As we know Spring Web MVC works based on MVC design pattern. Here C means Controller which is nothing but Front End Controller. Front End Controller will respond for each request for processing and send produced model to the View. Please find the below given diagram for Spring Web MVC Control/Request Processing Flow.

If you see the diagram, the request will receive by Dispatcher controller (Front End Controller). It will use HandlerMapping to get name of request handler I.e., Controller. Request will be delegated to the finalized Handler means Controller by Dispatcher servlet. Here Dispatcher servlet will take help of HandlerAdapter to invoke the Controller. The request will be processed to generate model by Controller. The produced model will be displayed in the view.
Dispatcher Servlet Configuration:
We discussed about Dispatcher servlet role in the previous section. The Dispatcher Servlet is the main Controller class that control the entire Control/Request processing flow. We can configured Dispatcher Servlet in two types.
XML Based Configuration (spring-dispatcher-servlet.xml):
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package = "com.docsconsole.tutorials.springmvc5.*" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/views/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
Here we used context:component-scan to activate the registered beans and will also scan to find the registered beans
Java Based Configuration (WebMvcConfig.java):
package com.docsconsole.tutorials.springmvc5.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.docsconsole.tutorials.springmvc5"})
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}
@Configuration will be used as source of beans definition. By fetching this class object which is annotated by @Configuration, spring will create the beans objects accordingly.
@EnableWebMvc will be used for enabling the MVC configuration. Means it will use to register beans which are related to MVC.
@ComponentScan will do same as context:component-scan.