Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
The DispatcherServlet
in a Spring MVC application serves as the front controller and handles incoming HTTP requests, delegating them to the appropriate controllers. In a traditional Spring MVC application (not Spring Boot), the DispatcherServlet
is usually configured in the web.xml
file.
Here's how you can configure the DispatcherServlet
in web.xml
:
Setup Your web.xml
File:
If your project doesn't already have a web.xml
, it should be located in the WEB-INF
directory of your web application.
Add DispatcherServlet
Configuration:
<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_3_1.xsd" version="3.1"> <!-- DispatcherServlet Configuration --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- This will look for a file named [servlet-name]-servlet.xml in the WEB-INF directory --> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Servlet Mapping Configuration --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Create dispatcher-servlet.xml
Configuration:
Based on the above configuration, Spring will look for a file named dispatcher-servlet.xml
in the WEB-INF
directory. This is where you define your Spring beans, controllers, view resolvers, etc.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <!-- Package to scan for @Controller and other annotated components --> <context:component-scan base-package="com.example.myapp" /> <!-- Define your view resolver, data sources, etc. here --> </beans>
Define Controllers:
With the above configuration, you can now define Spring MVC controllers in the package com.example.myapp
(or any other package you specified in dispatcher-servlet.xml
).
Remember, if you're using Spring Boot, the configuration is much simpler since it uses a convention-over-configuration paradigm and automatically configures the DispatcherServlet
for you. The above approach is more for traditional Spring MVC applications.
Configuring DispatcherServlet in web.xml example:
<!-- web.xml --> <web-app> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Setting up Dispatcher Servlet mapping in web.xml:
<!-- web.xml --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping>
Defining DispatcherServlet in web.xml for Spring MVC:
<!-- web.xml --> <servlet> <servlet-name>springMvcDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvcDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Web.xml configuration for Dispatcher Servlet in Spring:
<!-- web.xml --> <servlet> <servlet-name>myDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myDispatcherServlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
Dispatcher Servlet initialization parameters in web.xml:
<!-- web.xml --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-context.xml</param-value> </init-param> </servlet>
Mapping URL patterns for Dispatcher Servlet in web.xml:
<!-- web.xml --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
Defining Spring MVC servlet in web.xml file:
<!-- web.xml --> <servlet> <servlet-name>springMvcServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springMvcServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Configuring multiple Dispatcher Servlets in web.xml:
<!-- web.xml --> <servlet> <servlet-name>dispatcherServlet1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet1</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>dispatcherServlet2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet2</servlet-name> <url-pattern>/admin/*</url-pattern> </servlet-mapping>
Handling URL patterns with Dispatcher Servlet in web.xml:
<!-- web.xml --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/admin/*</url-pattern> </servlet-mapping>
Configuring context parameters for Dispatcher Servlet in web.xml:
<!-- web.xml --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-context.xml</param-value> </init-param> </servlet>
Web.xml setup for Spring MVC application with Dispatcher Servlet:
<!-- web.xml --> <servlet> <servlet-name>springMvcDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvcDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>