Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,9 +26,11 @@
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;
Expand Down Expand Up @@ -158,13 +158,6 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
//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
Expand All @@ -176,6 +169,7 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
dispatcher.forward(request, response);
} else {
LOG.debug("Including location: {}", finalLocation);

dispatcher.include(request, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,30 +38,38 @@ public void testForward() 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);
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 {
Expand All @@ -76,7 +83,6 @@ public void testWithParameter() throws Exception {

// 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
Expand Down