Browse Source

DefaultServletHttpRequestHandler supports DispatcherType.INCLUDE

See gh-30113
pull/31284/head
Andreas Bergander 2 years ago committed by rstoyanchev
parent
commit
732f0a6a77
  1. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java
  2. 19
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java

8
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/DefaultServletHttpRequestHandler.java

@ -18,6 +18,7 @@ package org.springframework.web.servlet.resource; @@ -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 @@ -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);
}
}
}

19
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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 {

Loading…
Cancel
Save