DispatcherServlet and web.xml in Spring Boot – Spring Boot中的DispatcherServlet和web.xml

最后修改: 2020年 11月 13日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

The DispatcherServlet is the front controller in Spring web applications. It’s used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web.xml file.

DispatcherServlet是Spring Web应用程序中的前台控制器。它被用来在Spring MVC中创建Web应用和REST服务。在传统的Spring Web应用中,这个Servlet被定义在web.xml文件中。

In this tutorial, we’ll migrate code from a web.xml file to DispatcherServlet in a Spring Boot application. Also, we’ll map Filter, Servlet, and Listener classes from web.xml to the Spring Boot application.

在本教程中,我们将把代码从web.xml文件迁移到Spring Boot应用程序中的DispatcherServlet。此外,我们将把FilterServletListener类从web.xml映射到Spring Boot应用程序中。

2. Maven Dependency

2.Maven的依赖性

First, we have to add the spring-boot-starter-web Maven dependency to our pom.xml file:

首先,我们必须将spring-boot-starter-web Maven依赖性添加到我们的pom.xml文件。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3. DispatcherServlet

3.DispatcherServlet

DispatcherServlet receives all of the HTTP requests and delegates them to controller classes.

DispatcherServlet接收所有的HTTP请求并将其委托给控制器类。

Before the Servlet 3.x specification, DispatcherServlet would be registered in the web.xml file for a Spring MVC application. Since the Servlet 3.x specification, we can register servlets programmatically using ServletContainerInitializer.

在Servlet 3.x规范之前,DispatcherServlet将在Spring MVC应用程序的web.xml文件中注册。自从Servlet 3.x规范之后,我们可以使用ServletContainerInitializer以编程方式注册Servlet。

Let’s see a DispatcherServlet example configuration in the web.xml file:

让我们看看DispatcherServlet文件中的配置示例。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Spring Boot provides the spring-boot-starter-web library for developing web applications using Spring MVC. One of the main features of Spring Boot is autoconfiguration. The Spring Boot autoconfiguration registers and configures the DispatcherServlet automatically. Therefore, we don’t need to register the DispatcherServlet manually.

Spring Boot提供了spring-boot-starter-web库,用于使用Spring MVC开发Web应用。Spring Boot的主要功能之一是自动配置。Spring Boot的自动配置会自动注册和配置DispatcherServlet。因此,我们不需要手动注册DispatcherServlet

By default, the spring-boot-starter-web starter configures DispatcherServlet to the URL pattern “/”. So, we don’t need to complete any additional configuration for the above DispatcherServlet example in the web.xml file. However, we can customize the URL pattern using server.servlet.* in the application.properties file:

默认情况下,spring-boot-starter-web启动器将DispatcherServlet配置为URL模式”/”。因此,我们不需要为上述DispatcherServlet示例在web.xml文件中完成任何额外的配置。然而,我们可以在application.properties文件中使用server.servlet.*自定义URL模式。

server.servlet.context-path=/demo
spring.mvc.servlet.path=/baeldung

With these customizations, DispatcherServlet is configured to handle the URL pattern /baeldung and the root contextPath will be /demo. Thus, DispatcherServlet listens at http://localhost:8080/demo/baeldung/.

通过这些定制,DispatcherServlet被配置为处理URL模式/baeldung,根contextPath将是/demo。因此,DispatcherServlethttp://localhost:8080/demo/baeldung/.上监听。

4. Application Configuration

4.应用配置

Spring MVC web applications use the web.xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web.xml file.

Spring MVC Web应用程序使用web.xml文件作为部署描述符文件。此外,它还定义了URL路径和web.xml文件中的servlets之间的映射关系。

This is no longer the case with Spring Boot. If we need a special filter, we can register it in a Java class configuration. The web.xml file includes filters, servlets, and listeners.

在Spring Boot中,情况不再是这样了。如果我们需要一个特殊的过滤器,我们可以在Java类配置中注册它。web.xml文件包括过滤器、Servlet和监听器。

When we want to migrate from a traditional Spring MVC to a modern Spring Boot application, how can we port our web.xml to a new Spring Boot application? In Spring Boot applications, we can add these concepts in several ways.

当我们想从传统的Spring MVC迁移到现代的Spring Boot应用程序时,如何将我们的web.xml移植到新的Spring Boot应用程序?在Spring Boot应用程序中,我们可以通过几种方式添加这些概念。

4.1. Registering a Filter

4.1.注册一个过滤器

Let’s create a filter by implementing the Filter interface:

让我们通过实现Filter接口创建一个过滤器。

@Component
public class CustomFilter implements Filter {

    Logger logger = LoggerFactory.getLogger(CustomFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
        logger.info("CustomFilter is invoked");
        chain.doFilter(request, response);
    }

    // other methods 
}

Without Spring Boot, we would configure our CustomFilter in the web.xml file:

如果没有Spring Boot,我们会在web.xml文件中配置我们的CustomFilter

<filter>
    <filter-name>customFilter</filter-name>
    <filter-class>CustomFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>customFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

In order for Spring Boot to be able to recognize a filter, we just needed to define it as a bean with the @Component annotation.

为了让Spring Boot能够识别过滤器,我们只需要把它定义为带有@Component注解的bean。

4.2. Registering a Servlet

4.2.注册一个Servlet

Let’s define a servlet by extending the HttpServlet class:

让我们通过扩展HttpServlet类来定义一个Servlet。

public class CustomServlet extends HttpServlet {

    Logger logger = LoggerFactory.getLogger(CustomServlet.class);

    @Override
    protected void doGet(
        HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException {
            logger.info("CustomServlet doGet() method is invoked");
            super.doGet(req, resp);
    }

    @Override
    protected void doPost(
        HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException {
            logger.info("CustomServlet doPost() method is invoked");
            super.doPost(req, resp);
    }
}

Without Spring Boot, we would configure our CustomServlet in the web.xml file:

如果没有Spring Boot,我们会在web.xml文件中配置我们的CustomServlet

<servlet>
    <servlet-name>customServlet</servlet-name>
    <servlet-class>CustomServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>customServlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>

In a Spring Boot application, the servlet is registered either as a Spring @Bean or by scanning the @WebServlet annotated classes with an embedded container.

在Spring Boot应用程序中,servlet被注册为Spring @Bean或通过扫描带有嵌入式容器的@WebServlet注释的类。

With the Spring @Bean approach, we can use the ServletRegistrationBean class to register the servlet.

通过Spring的@Bean方法,我们可以使用ServletRegistrationBean类来注册Servlet

So, we’ll define CustomServlet as a bean with the ServletRegistrationBean class:

因此,我们将把CustomServlet定义为ServletRegistrationBean类的一个bean。

@Bean
public ServletRegistrationBean customServletBean() {
    ServletRegistrationBean bean = new ServletRegistrationBean(new CustomServlet(), "/servlet");
    return bean;
}

4.3. Registering a Listener

4.3.注册一个监听器

Let’s define a listener by extending the ServletContextListener class:

让我们通过扩展ServletContextListener类来定义一个监听器。

public class CustomListener implements ServletContextListener {

    Logger logger = LoggerFactory.getLogger(CustomListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("CustomListener is initialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("CustomListener is destroyed");
    }
}

Without Spring Boot, we would configure our CustomListener in the web.xml file:

如果没有Spring Boot,我们会在web.xml文件中配置我们的CustomListener

<listener>
    <listener-class>CustomListener</listener-class>
</listener>

To define a listener in a Spring Boot application, we can use either the @Bean or @WebListener annotations.

要在Spring Boot应用程序中定义一个监听器,我们可以使用@Bean@WebListener注解。

With the Spring @Bean approach, we can use the ServletListenerRegistrationBean class to register the Listener.

通过Spring @Bean方法,我们可以使用ServletListenerRegistrationBean类来注册Listener

So, let’s define CustomListener as a bean with the ServletListenerRegistrationBean class:

因此,让我们将CustomListener定义为ServletListenerRegistrationBean类的一个bean。

@Bean
public ServletListenerRegistrationBean<ServletContextListener> customListenerBean() {
    ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean();
    bean.setListener(new CustomListener());
    return bean;
}

Upon starting our application, we can check the log output to see confirmation that the listener has been successfully initialized:

在启动我们的应用程序时,我们可以检查日志输出,看是否确认监听器已被成功初始化。

2020-09-28 08:50:30.872 INFO 19612 --- [main] c.baeldung.demo.listener.CustomListener: CustomListener is initialized

5. Conclusion

5.总结

In this quick tutorial, we saw how to define DispatcherServlet and web.xml elements including filter, servlet, and listener in a Spring Boot application. And, as always, the source code for the above example can be found over on GitHub.

在这个快速教程中,我们看到了如何在Spring Boot应用程序中定义DispatcherServletweb.xml元素,包括filterservletlistener。而且,像往常一样,上述示例的源代码可以在GitHub上找到over