struts2中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()-->
我还想知道第五点在 stack.getContex().put()
和 stack.set()
中如何工作?据我了解,在第 6 个中,我访问的 resultDTO 是一个不同的根,而在第 7 个中,它是默认根(即 ValueStack)的子级。第八步开始从默认根目录开始搜索。
我浏览了 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 并使此链接相当混乱http://www.opensymphony.com/ognl/html/DeveloperGuide/introduction.html#embeddingOGNL
说了这么多,我不太倾向于使用 stack.getContext().put() 方法,因为我可以通过将 url 设置为 ?debug=browser 来清楚地看到其中的值。如果我做错了,请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ValueStack
是针对每个请求的。如果将值放在堆栈上,则可以稍后在请求中(即在视图层中)访问它们,但无法在重定向中幸存,这将是一个新的 HTTP 请求并具有自己的 ValueStack 。在正常情况下,URL 或表单帖子中的参数将使用操作的 setter 方法在操作上设置。在拦截器中,您可以直接将值添加到堆栈中。例如,
ExceptionMappingInterceptor
使用stack.push(Object)
方法发布异常以在错误页面上使用。stack.getContext().put(String, Object)
-- 将键/值放入位于堆栈上的映射中。该映射代表堆栈的上下文。stack.set(String, Object)
-- 将键/值放入位于堆栈上的映射中。我不确定这与之前的方法有何关系,除了它是不同的地图之外。您不需要从视图层内将任何内容放置在堆栈上,所以我很好奇您想要做什么需要这样做。
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 ownValueStack
.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 thestack.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.