Value stack is a fundamental part of XWork2. As XWork2 processed a command request, objects of interest could be pushed into the value stack or set into its context. This could be done by XWork2 itself, Action, Interceptors, Results etc.

XWork2 Action itself is actually pushed into the stack during the invocation process.

Working Concept

There are two ways objects could be store in XWork2's value stack.

Top of the value stack

Objects get push into XWork2 value stack in a first-in-last-out fashion just like any ordinary stack would.

To push an Object into XWork's value stack, simply do

valueStack.push(anObject);

Actually XWork2 uses Ognl underneath. By having a CompoundRoot that allows objects to be stack up on as Ognl root, the effect of a stack is achieved.

When value the stack is queried for object using the followng method signature eg.

valueStack.findString(String);
 valueStack.findValue(String);
 valueStack.findValue(String, Class);

the objects residing the XWork2 value stack will be search accordingly (with those in the top of the stack having higher precedence).

For example, with the following value stack,

Value Stack
objectA (foo.bar.ObjectA)
  • method0ne
  • method1
  • method2
objectB (foo.bar.ObjectB)
  • methodTwo
  • method3
  • method4
objectC (foo.bar.ObjectC)
  • methodOne
  • methodTwo
  • method5
  • method6

Case 1

Object o = findValue("method2");

In this case, the "methodA" of instance foo.bar.ObjectA will be invoked with its returning object returned.

Case 2

Object o = findValue("method6");

In this case, the "method6" of instance foo.bar.ObjectC will be invoked with its returning object returned.

Case 3

Object o = findValue("methodOne");

In this case, the "methodOne" of instance foo.bar.ObjectA will be invoked with its returning object returned. This is due to instance of foo.bar.ObjectA being on the top of the stack compared to instance of foo.bar.ObjectC, XWork2 searches down the stack and hence will find instance of foo.bar.ObjectA first.

Case 4

Object o = fincValue("methodTwo");

In this case, the "methodTwo" of instance foo.bar.ObjectB will be invoked with its returning object returned. This is due to instance of foo.bar.ObjectB being on the top of the stack compared to instance of foo.bar.ObjectC, XWork2 searches down the the stack and hence will find instance of foo.bar.ObjectB first.

Value stack's context

To store an object in XWork2's value stack's context, one could use

Map context = valueStack.getContext();
 context.put("key", someObject);

To query Object from XWork2's value stack's context, one could use

valueStack.findValue("#key");

or

valueStack.getContext().get("key");