Skip to content

Commit d27d3fb

Browse files
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 <[email protected]> * WW-5548 Drops RequestDispatcher parameters as they should be defined by Servlet container --------- Co-authored-by: Copilot <[email protected]>
1 parent f66e078 commit d27d3fb

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

core/src/main/java/org/apache/struts2/result/ServletDispatcherResult.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919
package org.apache.struts2.result;
2020

21-
import org.apache.struts2.ActionInvocation;
22-
import org.apache.struts2.inject.Inject;
2321
import jakarta.servlet.RequestDispatcher;
2422
import jakarta.servlet.http.HttpServletRequest;
2523
import jakarta.servlet.http.HttpServletResponse;
@@ -28,9 +26,11 @@
2826
import org.apache.commons.lang3.StringUtils;
2927
import org.apache.logging.log4j.LogManager;
3028
import org.apache.logging.log4j.Logger;
29+
import org.apache.struts2.ActionInvocation;
3130
import org.apache.struts2.ServletActionContext;
3231
import org.apache.struts2.StrutsStatics;
3332
import org.apache.struts2.dispatcher.HttpParameters;
33+
import org.apache.struts2.inject.Inject;
3434
import org.apache.struts2.url.QueryStringParser;
3535

3636
import java.io.Serial;
@@ -158,13 +158,6 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
158158
//if we are inside an action tag, we always need to do an include
159159
boolean insideActionTag = (Boolean) ObjectUtils.defaultIfNull(request.getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION), Boolean.FALSE);
160160

161-
// this should allow integration with third-party view related frameworks
162-
if (finalLocation.contains("?")) {
163-
request.setAttribute(RequestDispatcher.FORWARD_SERVLET_PATH, finalLocation.substring(0, finalLocation.indexOf('?')));
164-
} else {
165-
request.setAttribute(RequestDispatcher.FORWARD_SERVLET_PATH, finalLocation);
166-
}
167-
168161
// If we're included, then include the view
169162
// Otherwise do forward
170163
// This allow the page to, for example, set content type
@@ -176,6 +169,7 @@ public void doExecute(String finalLocation, ActionInvocation invocation) throws
176169
dispatcher.forward(request, response);
177170
} else {
178171
LOG.debug("Including location: {}", finalLocation);
172+
179173
dispatcher.include(request, response);
180174
}
181175
}

core/src/test/java/org/apache/struts2/result/ServletDispatcherResultTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.struts2.result;
2020

21-
import jakarta.servlet.RequestDispatcher;
2221
import org.apache.struts2.ActionContext;
2322
import org.apache.struts2.StrutsInternalTestCase;
2423
import org.apache.struts2.StrutsStatics;
@@ -39,30 +38,38 @@ public void testForward() throws Exception {
3938
ServletDispatcherResult view = new ServletDispatcherResult();
4039
view.setLocation("foo.jsp");
4140

41+
request.setRequestURI("/app/namespace/my.action");
42+
request.setContextPath("/app");
43+
request.setServletPath("/namespace/my.action");
44+
request.setPathInfo(null);
45+
request.setQueryString("a=1&b=2");
46+
4247
request.setAttribute("struts.actiontag.invocation", null);
4348
request.setAttribute("jakarta.servlet.include.servlet_path", null);
44-
request.setRequestURI("foo.jsp");
4549

4650
response.setCommitted(Boolean.FALSE);
4751

4852
view.execute(invocation);
4953

5054
assertEquals("foo.jsp", response.getForwardedUrl());
51-
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
5255
}
5356

5457
public void testInclude() throws Exception {
5558
ServletDispatcherResult view = new ServletDispatcherResult();
5659
view.setLocation("foo.jsp");
5760

61+
request.setRequestURI("/app/namespace/my.action");
62+
request.setContextPath("/app");
63+
request.setServletPath("/namespace/my.action");
64+
request.setPathInfo(null);
65+
request.setQueryString("a=1&b=2");
66+
5867
request.setAttribute("struts.actiontag.invocation", null);
5968
response.setCommitted(Boolean.TRUE);
60-
request.setRequestURI("foo.jsp");
6169

6270
view.execute(invocation);
6371

6472
assertEquals("foo.jsp", response.getIncludedUrl());
65-
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
6673
}
6774

6875
public void testWithParameter() throws Exception {
@@ -76,7 +83,6 @@ public void testWithParameter() throws Exception {
7683

7784
// See https://issues.apache.org/jira/browse/WW-5486
7885
assertEquals("1", stack.findString("#parameters.bar"));
79-
assertEquals("foo.jsp", request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
8086
}
8187

8288
@Override

0 commit comments

Comments
 (0)