在struts2中应用程序的ValueStack生命周期是什么?

时间:2011-02-18 04:19:56

标签: struts2 ognl xwork valuestack

我可以通过多种方式在ValueStack上设置属性。

 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

我需要能够在JSP,freemarker和java中获取所有这些值,如

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

我想知道这4种设置方法中每种方法的生命周期。它是跨应用程序。是否为每个请求创建了 ValueStack ,并为每个请求设置了应用程序和会话值?

我知道第四种方法是最常用的方法,但我可能不会在所有地方使用它,因为动作类不易被访问。

我对在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()-->

我还想了解第5点在stack.getContex().put()stack.set()中的运作方式?据我所知,在第6个我正在访问的 resultDTO 是一个不同的根,而在第7个,它是默认根的子节点,即ValueStack。在第8天,它开始从默认的根搜索。

我经历了http://struts.apache.org/2.0.11.1/docs/ognl.htmlhttp://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/util/ValueStack.html而非常混淆此链接http://www.opensymphony.com/ognl/html/DeveloperGuide/introduction.html#embeddingOGNL

说完所有这些之后我不太倾向于使用stack.getContext().put()方法,因为我可以通过将网址设置为?debug = browser 来清楚地看到值。如果我出错了,请告诉我。

1 个答案:

答案 0 :(得分:0)

ValueStack是每次请求。如果您将值放在堆栈上,它们可以在请求中稍后访问(即在视图层中),但不会在重定向中存在,这将是一个新的HTTP请求并且具有自己的ValueStack

在正常情况下,将使用操作的setter方法在操作上设置URL或表单中的参数。在拦截器中,您可以直接向堆栈添加值。例如,ExceptionMappingInterceptor使用stack.push(Object)方法发布在错误页面上使用的异常。

  • stack.getContext().put(String, Object) - 将键/值放入堆栈中的地图中。地图代表堆栈的上下文。
  • stack.set(String, Object) - 将键/值放入堆栈中的地图中。我不确定这与之前的方法有什么关系,除了它是一个不同的地图。
  • stack.push(Object) - 这会将对象放在堆栈的根目录上。

你不需要在视图层内放置任何东西,所以我很好奇你想要做什么,这需要它。