WW-5548 Defines proper request attributes when forwarding or including final path (#1265)

* WW-5548 Defines proper request attributes when forwarding or including final path

* Fies typo

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* WW-5548 Drops RequestDispatcher parameters as they should be defined by Servlet container

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Lukasz Lenart
2025-05-12 14:26:42 +02:00
committed by GitHub
parent f66e078f10
commit d27d3fba3a
2 changed files with 15 additions and 15 deletions
@@ -18,8 +18,6 @@
*/
package org.apache.struts2.result;
import org.apache.struts2.ActionInvocation;
import org.apache.struts2.inject.Inject;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -28,9 +26,11 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ActionInvocation;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.inject.Inject;
import org.apache.struts2.url.QueryStringParser;
import java.io.Serial;
@@ -158,13 +158,6 @@ public class ServletDispatcherResult extends StrutsResultSupport {
//if we are inside an action tag, we always need to do an include
boolean insideActionTag = (Boolean) ObjectUtils.defaultIfNull(request.getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION), Boolean.FALSE);
// this should allow integration with third-party view related frameworks
if (finalLocation.contains("?")) {
request.setAttribute(RequestDispatcher.FORWARD_SERVLET_PATH, finalLocation.substring(0, finalLocation.indexOf('?')));
} else {
request.setAttribute(RequestDispatcher.FORWARD_SERVLET_PATH, finalLocation);
}
// If we're included, then include the view
// Otherwise do forward
// This allow the page to, for example, set content type
@@ -176,6 +169,7 @@ public class ServletDispatcherResult extends StrutsResultSupport {
dispatcher.forward(request, response);
} else {
LOG.debug("Including location: {}", finalLocation);
dispatcher.include(request, response);
}
}
@@ -18,7 +18,6 @@
*/
package org.apache.struts2.result;
import jakarta.servlet.RequestDispatcher;
import org.apache.struts2.ActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.StrutsStatics;
@@ -39,30 +38,38 @@ public class ServletDispatcherResultTest extends StrutsInternalTestCase implemen
ServletDispatcherResult view = new ServletDispatcherResult();
view.setLocation("foo.jsp");
request.setRequestURI("/app/namespace/my.action");
request.setContextPath("/app");
request.setServletPath("/namespace/my.action");
request.setPathInfo(null);
request.setQueryString("a=1&b=2");
request.setAttribute("struts.actiontag.invocation", null);
request.setAttribute("jakarta.servlet.include.servlet_path", null);
request.setRequestURI("foo.jsp");
response.setCommitted(Boolean.FALSE);
view.execute(invocation);
assertEquals("foo.jsp", response.getForwardedUrl());
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
}
public void testInclude() throws Exception {
ServletDispatcherResult view = new ServletDispatcherResult();
view.setLocation("foo.jsp");
request.setRequestURI("/app/namespace/my.action");
request.setContextPath("/app");
request.setServletPath("/namespace/my.action");
request.setPathInfo(null);
request.setQueryString("a=1&b=2");
request.setAttribute("struts.actiontag.invocation", null);
response.setCommitted(Boolean.TRUE);
request.setRequestURI("foo.jsp");
view.execute(invocation);
assertEquals("foo.jsp", response.getIncludedUrl());
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
}
public void testWithParameter() throws Exception {
@@ -76,7 +83,6 @@ public class ServletDispatcherResultTest extends StrutsInternalTestCase implemen
// See https://issues.apache.org/jira/browse/WW-5486
assertEquals("1", stack.findString("#parameters.bar"));
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
}
@Override