struts2 - understanding the value stack

dcp picture dcp · Nov 26, 2009 · Viewed 34.2k times · Source

I have a question regarding the struts2 value stack. Let's say I have an Action class called RegisterAction that has an execute method as follows:

public String execute() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(new String("test string"));
    return SUCCESS;
}

My struts.xml looks like this:

<struts>
    <package name="default" extends="struts-default">
        <action name="*Register" method="{1}" class="vaannila.RegisterAction">
            <result name="populate">/register.jsp</result>
            <result name="input">/register.jsp</result>
            <result name="success">/success.jsp</result>
        </action>        
        <action name="*Test" method="{1}" class="vaannila.TestAction">
            <result name="test">/test.jsp</result>
            <result name="success">/success2.jsp</result>
        </action>        
    </package>
</struts>

So control will flow to the success.jsp after the execute method executes in that class.

My questions are:

1) how do I get that value I pushed on the stack in the success.jsp?

2) Let's say in success.jsp I have a <s:submit method="testMethod" /> that redirects to an action class called TestAction. In other words, from the Register page, the user clicks submit, and in the execute method of the RegisterAction we push the "test string" on the stack. Then we go to success.jsp. The success.jsp has a submit button that directs us to TestAction#testMethod. In TestAction#testMethod, is the value I pushed on the stack in RegisterAction#execute still there? How can I get it? I stepped through the eclipse debugger but I don't see the value.

Thanks.

Answer

dcp picture dcp · Nov 27, 2009

Ok, I figured this out.

1) The way I found to get objects on the value stack so we can access them from a jsp is like this:

Map<String, Object> context = new HashMap<String, Object>();
context.put("key", "some object");
context.put("key2", "another object");
ActionContext.getContext().getValueStack().push(context);

In other words, we can put a HashMap on the value stack containing the objects we need. Then, in the jsp, we can access the actual values like this:

<s:property value="key" />
<s:property value="key2" />

It will look through the value stack and find those values in the HashMap we pushed.

2) An instance of the action class is associated with just one request. So when we go to another action and then end up at another jsp, the stuff we pushed on the value stack from the first action won't be there since the other action has it's own value stack. reference: http://www.manning-sandbox.com/thread.jspa?messageID=93045

You guys can feel free to correct me if any of this is wrong or if there are smarter ways to do these things :).

Thanks.