ValueStack生命周期是否在struts2中的整个应用程序中? [英] Is the ValueStack life cycle across the application in struts2?

查看:115
本文介绍了ValueStack生命周期是否在struts2中的整个应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过几种方式在ValueStack上设置属性.

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

我需要能够在JSP,freemarker和java之类的

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

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

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

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.

对于在JSP中进行访问,我还有另一个疑问

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()-->

我还想知道第5点在stack.getContex().put()stack.set()中如何工作?我知道在第六个访问的 resultDTO 是一个不同的根,而在第七个访问中,它是默认根的子级,即ValueStack.从8号开始,它会从默认根目录开始搜索.

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.

我经历了 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 ,而使此链接

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

所有这些我不太愿意使用stack.getContext().put()方法,因为通过将URL设置为?debug = browser 可以清楚地看到其中的值.如果我做错了,请通知我.

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.

推荐答案

ValueStack是按请求的.如果将值放在堆栈上,则可以在请求的后面(即,在视图层中)访问它们,但不能在重定向中幸存下来,重定向将是一个新的HTTP请求,并且具有自己的ValueStack.

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.

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

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)-将键/值放入存在于堆栈中的映射中.该映射表示堆栈的上下文.
  • stack.set(String, Object)-将键/值放入存在于堆栈中的映射中.我不确定这与以前的方法有什么关系,除了它是一张不同的地图.
  • stack.push(Object)-将对象放置在堆栈的根上.
  • 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.

这篇关于ValueStack生命周期是否在struts2中的整个应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆