PGD
Posted on March 7, 2024
The DispatcherServlet is a front controller serving as a single point entry of request. In Spring MVC architecture, every request passes through DispatcherServlet. Once the DispatcherServlet receives the request then it recognizes the path, gets a controller mapped to the path, and lets the controller process the business logic.
Since DispatcherServlet is a front controller, which is a single point entry, it's the only servlet to register in web application context. We can register it by specifying inside of web.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Also it's possible to tell DispatcherServlet where the Spring config file is by specifying an whose param name is "contextConfigLocation". If I have a Spring config file named "context-common.xml" on the root directory of web application, then I can specify the location as follows.
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/context-common.xml</param-value>
</init-param
Also I can put the config file at the resources file and prepend "classpath:" at the config file path so that DispatcherServlet knows the config file places on the classpath.
DispatcherServlet lookups spring beans of controllers using WebApplicationContext. By default, its implementation the DispatcherServlet is using is org.springframework.web.context.support.XmlWebApplcationContext. That receives an XML configuration file for initial config file.
If I want to use a Java class annotated with @Configuration
to configure Spring context, I have to say to DispatcherServlet that it should make use of other implementation of WebApplicationContext. AnnotationWebConfigApplicationContext is needed in order for the Spring Container to contain the configuration bean. It is possible to configure that in web.xml:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.springmvc.demo.web.config.WebMVCConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Posted on March 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.