This is an automated email from the ASF dual-hosted git repository.

kusal pushed a commit to branch WW-5379-velocity-stack-alt
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 3dbc5c250e42e2ab33a9674eb2049e879a23e519
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Thu Dec 28 05:00:31 2023 +1100

    WW-5379 Implement alternative mechanism for Velocity directives to obtain 
ValueStack
---
 .../views/velocity/StrutsVelocityContext.java      |  4 +++
 .../velocity/components/AbstractDirective.java     | 31 +++++++++++++---------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java
 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java
index 4241f6ead..587acb013 100644
--- 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java
+++ 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java
@@ -103,4 +103,8 @@ public class StrutsVelocityContext extends VelocityContext {
         }
         return null;
     }
+
+    public ValueStack getValueStack() {
+        return stack;
+    }
 }
diff --git 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
index e955b32aa..d007ab9da 100644
--- 
a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
+++ 
b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/components/AbstractDirective.java
@@ -18,16 +18,12 @@
  */
 package org.apache.struts2.views.velocity.components;
 
-import java.io.IOException;
-import java.io.Writer;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.opensymphony.xwork2.inject.Container;
+import com.opensymphony.xwork2.util.ValueStack;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.components.Component;
+import org.apache.struts2.views.util.ContextUtil;
+import org.apache.struts2.views.velocity.StrutsVelocityContext;
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
@@ -35,9 +31,12 @@ import 
org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.directive.Directive;
 import org.apache.velocity.runtime.parser.node.Node;
 
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.inject.Container;
-import com.opensymphony.xwork2.util.ValueStack;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
 
 public abstract class AbstractDirective extends Directive {
     public String getName() {
@@ -57,8 +56,14 @@ public abstract class AbstractDirective extends Directive {
     protected abstract Component getBean(ValueStack stack, HttpServletRequest 
req, HttpServletResponse res);
 
     public boolean render(InternalContextAdapter ctx, Writer writer, Node 
node) throws IOException, ResourceNotFoundException, ParseErrorException, 
MethodInvocationException {
-        // get the bean
-        ValueStack stack = (ValueStack) ctx.get("stack");
+        ValueStack stack;
+        if (ctx.getInternalUserContext() instanceof StrutsVelocityContext) {
+            StrutsVelocityContext svc = (StrutsVelocityContext) 
ctx.getInternalUserContext();
+            stack = svc.getValueStack();
+        } else {
+            // Fallback to assuming the ValueStack was put into the Velocity 
context (as is by default)
+            stack = (ValueStack) ctx.get(ContextUtil.STACK);
+        }
         HttpServletRequest req = (HttpServletRequest) 
stack.getContext().get(ServletActionContext.HTTP_REQUEST);
         HttpServletResponse res = (HttpServletResponse) 
stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
         Component bean = getBean(stack, req, res);

Reply via email to