DispatcherServlet in Spring MVC
The DispatcherServlet
is the front controller in the Spring MVC framework. It is responsible for handling all incoming HTTP requests and dispatching them to the appropriate handlers, such as controllers, views, and other components. This guide covers the key concepts and configuration of the DispatcherServlet
.
Key Concepts of DispatcherServlet
- Front Controller: A central point for handling all HTTP requests in a Spring MVC application.
- Handler Mapping: Determines which handler (controller) should process a given request.
- Handler Adapter: Invokes the handler mapped to the request.
- View Resolver: Resolves logical view names to actual view implementations.
Configuring the DispatcherServlet
To configure the DispatcherServlet
, you need to define it in the web.xml
file:
web.xml
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring Configuration File
Create a Spring configuration file to define beans and enable Spring MVC:
dispatcher-config.xml
<?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">
<mvc:annotation-driven />
<context:component-scan base-package="com.example.springmvc.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Creating a Controller
Create a controller class to handle incoming requests:
HomeController.java
// HomeController.java
package com.example.springmvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Welcome to Spring MVC!");
return "home";
}
}
Setting Up View Templates
Create JSP files or other view templates to render the response:
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
DispatcherServlet Workflow
The DispatcherServlet
follows a specific workflow to process requests:
- Receives the HTTP request.
- Uses Handler Mapping to determine which handler (controller) should process the request.
- Uses Handler Adapter to invoke the handler.
- The handler processes the request and returns a ModelAndView object.
- The
DispatcherServlet
uses the View Resolver to resolve the logical view name to an actual view implementation. - The view is rendered and the response is returned to the client.
Key Points
- Front Controller: A central point for handling all HTTP requests in a Spring MVC application.
- Handler Mapping: Determines which handler (controller) should process a given request.
- Handler Adapter: Invokes the handler mapped to the request.
- View Resolver: Resolves logical view names to actual view implementations.
- Configure the
DispatcherServlet
in theweb.xml
file. - Create a Spring configuration file to define beans and enable Spring MVC.
- Create controller classes to handle incoming requests.
- Set up view templates to render the response.
- Understand the workflow of the
DispatcherServlet
to process requests and generate responses.
Conclusion
The DispatcherServlet
is the core component of the Spring MVC framework, acting as the front controller for handling all HTTP requests. By configuring the DispatcherServlet
and understanding its workflow, developers can build robust and maintainable web applications with Spring MVC. Happy coding!