Is the ValueStack life cycle across the application in struts2?

3.2k views Asked by At

I can set a property on ValueStack in several ways.

 ValueStack stack = ActionContext.getContext().getValueStack();
 stack.getContext().put("resultDTO",resultDTO);  //1. creates a different branch 
 //parallel to root

 stack.set("resultDTO", resultDTO); //2. pushes on root as a Map?
 stack.push(resultDTO); //3. pushes on root
 myActionClass.setProperty(); //4. normal action accessor

I need to be able to get all these values back in JSP, freemarker and java like

 stack.findValue() or stack.findString().    

I want to know about the life cycle of each of these 4 setting methods. Is it across application. Is the ValueStack created every request and the application and session values are set in it for every request?

I know the 4th method is the most common approach but i may not be using that in all places, where action class is not easily accessible.

I have another doubt about accessing in JSP

 <s:push value="resultDTO" ><s:property value="data.form1[0]" /></s:push>
 <!--5.works for context.put() & stack.set() both-->

 <s:property value="#resultDTO.data.form1[0].countryofissue" /> <!--6.context.put()-->
 <s:property value="resultDTO.data.form1[0].countryofissue" />  <!--7.stack.set()-->
 <s:property value="data.form1[0].countryofissue" />            <!--8.stack.push()-->

I also want to know how 5th point works in both stack.getContex().put() and stack.set()? I understand that in 6th the resultDTO I am accessing, is a different root and in 7th, it's the child of default root, which is ValueStack. In 8th it starts to search from default root.

I went through http://struts.apache.org/2.0.11.1/docs/ognl.html, http://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/util/ValueStack.html and rather confusing this link http://www.opensymphony.com/ognl/html/DeveloperGuide/introduction.html#embeddingOGNL

Having said all these I am little inclined to using stack.getContext().put() method as I can clearly see the values in by setting the url as ?debug=browser. Advise me if I am going wrong.

1

There are 1 answers

2
Steven Benitez On BEST ANSWER

The ValueStack is per-request. If you place values on the stack, they are accessible later in the request (i.e., in the view layer), but would not survive a redirect, which would be a new HTTP request and have its own ValueStack.

Under normal conditions, parameters in the URL or in a form post would be set on the action using the action's setter methods. In an interceptor, you can add values directly to the stack. For example, the ExceptionMappingInterceptor uses the stack.push(Object) method to publish exceptions for use on error pages.

  • stack.getContext().put(String, Object) -- Places the key/value into a map that lives on the stack. The map represents the context of the stack.
  • stack.set(String, Object) -- Places the key/value into a map that lives on the stack. I'm not sure how this relates to the previous method, other than it is a different map.
  • stack.push(Object) -- This places the object on the root of the stack.

You shouldn't need to place anything on the stack from within the view layer, so I'm curious what you are trying to do that necessitates that.