From 732f0a6a77b5bd30829df9414620356486575b97 Mon Sep 17 00:00:00 2001 From: Andreas Bergander Date: Mon, 13 Mar 2023 14:47:51 +0100 Subject: [PATCH] DefaultServletHttpRequestHandler supports DispatcherType.INCLUDE See gh-30113 --- .../DefaultServletHttpRequestHandler.java | 8 +++++++- .../DefaultServletHandlerConfigurerTests.java | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java index 25e218d081..d2ed439e4e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java @@ -18,6 +18,7 @@ package org.springframework.web.servlet.resource; import java.io.IOException; +import jakarta.servlet.DispatcherType; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; @@ -125,7 +126,12 @@ public class DefaultServletHttpRequestHandler implements HttpRequestHandler, Ser throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" + this.defaultServletName + "'"); } - rd.forward(request, response); + if (request.getDispatcherType() == DispatcherType.INCLUDE) { + rd.include(request, response); + } + else { + rd.forward(request, response); + } } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java index 169ca70a4f..1cc33984b9 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java @@ -16,6 +16,7 @@ package org.springframework.web.servlet.config.annotation; +import jakarta.servlet.DispatcherType; import jakarta.servlet.RequestDispatcher; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -88,6 +89,24 @@ public class DefaultServletHandlerConfigurerTests { assertThat(response.getForwardedUrl()).as("The request was not forwarded").isEqualTo(expected); } + @Test + public void handleIncludeRequest() throws Exception { + configurer.enable(); + SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping(); + DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**"); + + assertThat(handler).isNotNull(); + assertThat(handlerMapping.getOrder()).isEqualTo(Integer.MAX_VALUE); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setDispatcherType(DispatcherType.INCLUDE); + handler.handleRequest(request, response); + + String expected = "default"; + assertThat(servletContext.url).as("The ServletContext was not called with the default servlet name").isEqualTo(expected); + assertThat(response.getIncludedUrl()).as("The request was not included").isEqualTo(expected); + } + private static class DispatchingMockServletContext extends MockServletContext {