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

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

commit c5cf3fc28824f33ed2020464e0d9b7e1ea86fb82
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Mon Jan 8 20:47:35 2024 +1100

    WW-5379 Add one more method to provide Velocity directives with ValueStack
---
 .../views/velocity/components/AbstractDirective.java | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

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 539f64bdd..c662bf5a9 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,6 +18,7 @@
  */
 package org.apache.struts2.views.velocity.components;
 
+import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.inject.Container;
 import com.opensymphony.xwork2.util.ValueStack;
 import org.apache.struts2.ServletActionContext;
@@ -59,11 +60,7 @@ 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 {
-        ValueStack stack = extractValueStack(ctx);
-        if (stack == null) {
-            // Fallback to assuming the ValueStack was put into the Velocity 
context (as is by default)
-            stack = (ValueStack) ctx.get(ContextUtil.STACK);
-        }
+        ValueStack stack = getValueStack(ctx);
         HttpServletRequest req = (HttpServletRequest) 
stack.getContext().get(ServletActionContext.HTTP_REQUEST);
         HttpServletResponse res = (HttpServletResponse) 
stack.getContext().get(ServletActionContext.HTTP_RESPONSE);
         Component bean = getBean(stack, req, res);
@@ -84,6 +81,19 @@ public abstract class AbstractDirective extends Directive {
         return true;
     }
 
+    protected ValueStack getValueStack(Context context) {
+        ValueStack stack = extractValueStack(context);
+        if (stack == null) {
+            // Fallback to assuming the ValueStack was put into the Velocity 
context (as is by default)
+            stack = (ValueStack) context.get(ContextUtil.STACK);
+        }
+        if (stack == null) {
+            // Fallback to current ActionContext
+            stack = ActionContext.getContext().getValueStack();
+        }
+        return stack;
+    }
+
     private ValueStack extractValueStack(Context context) {
         do {
             if (context instanceof ValueStackProvider) {

Reply via email to