Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struts2 - understanding the value stack

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.

like image 932
dcp Avatar asked Nov 26 '09 15:11

dcp


People also ask

What is the default object of value stack?

These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes. Find a value by evaluating the given expression against the stack in the default search order.

What is ActionContext in struts2?

The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc. The ActionContext is thread local which means that values stored in the ActionContext are unique per thread.

What is ActionSupport in struts2?

public class ActionSupport extends Object implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable. Provides a default implementation for the most common actions. See the documentation for all the interfaces this class implements for more detailed information.

What is OGNL used for?

OGNL uses Java reflection and introspection to address the Object Graph of the runtime application. This allows the program to change behavior based on the state of the object graph instead of relying on compile time settings. It also allows changes to the object graph.


3 Answers

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.

like image 115
dcp Avatar answered Oct 19 '22 22:10

dcp


Struts 2 adds your action to the top of the value stack when executed. So, the usual way to put stuff on the Value Stack is to add getters/setters for the values to your Action class. You still use the s:property tag to access the values.

A CRUD tutorial: http://struts.apache.org/2.1.6/docs/crud-demo-i.html

like image 44
Nate Avatar answered Oct 19 '22 22:10

Nate


just define a property like

String string1 = "test string";

in your action.

in jsp you can access directly.

e.g

 <s:property value="string1"/>

will print out

"test string"

like image 25
Anil Punia Avatar answered Oct 19 '22 22:10

Anil Punia