Java struts2 - 理解价值栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1804266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
struts2 - understanding the value stack
提问by dcp
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:
我有一个关于 struts2 值堆栈的问题。假设我有一个 Action 类RegisterAction
,它有一个 execute 方法,如下所示:
public String execute() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new String("test string"));
return SUCCESS;
}
My struts.xml looks like this:
我的 struts.xml 看起来像这样:
<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.
因此,在该类中执行execute 方法后,控制将流向success.jsp。
My questions are:
我的问题是:
1) how do I get that value I pushed on the stack in the success.jsp
?
1)我如何获得我在堆栈中推送的值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.
2)假设success.jsp
我有一个<s:submit method="testMethod" />
重定向到一个名为TestAction
. 也就是说,在Register页面,用户点击submit,在execute方法中RegisterAction
我们将“测试字符串”压入栈中。然后我们去success.jsp
。该success.jsp
有一个提交按钮,指导我们TestAction#testMethod
。在TestAction#testMethod
,我压入堆栈的值RegisterAction#execute
还在吗?我怎么才能得到它?我单步调试了 Eclipse 调试器,但没有看到值。
Thanks.
谢谢。
采纳答案by dcp
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:
1)我发现在值堆栈上获取对象以便我们可以从jsp访问它们的方式是这样的:
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:
换句话说,我们可以在包含我们需要的对象的值堆栈上放置一个 HashMap。然后,在 jsp 中,我们可以像这样访问实际值:
<s:property value="key" />
<s:property value="key2" />
It will look through the value stack and find those values in the HashMap we pushed.
它将查看值堆栈并在我们推送的 HashMap 中找到这些值。
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
2) action 类的一个实例只与一个请求相关联。所以当我们去另一个动作然后在另一个 jsp 结束时,我们从第一个动作推入值堆栈的东西将不存在,因为另一个动作有它自己的值堆栈。参考: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.
谢谢。
回答by Nate
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.
Struts 2 在执行时将您的操作添加到值堆栈的顶部。因此,将内容放入值堆栈的常用方法是为 Action 类添加值的 getter/setter。您仍然使用 s:property 标签来访问这些值。
A CRUD tutorial: http://struts.apache.org/2.1.6/docs/crud-demo-i.html
CRUD 教程:http: //struts.apache.org/2.1.6/docs/crud-demo-i.html
回答by fool4jesus
Normally, as Nate says, you will use a field in your action, since the action is always on the ValueStack. However, this doesn't work if you're writing interceptor code since the interceptor will be gone by the time the template (JSP/freemarker etc) is invoked. There you need to put some kind of java bean-like object on the ValueStack, just as you do above.
通常,正如 Nate 所说,您将在操作中使用一个字段,因为该操作始终位于 ValueStack 上。但是,如果您正在编写拦截器代码,这将不起作用,因为在调用模板(JSP/freemarker 等)时拦截器将消失。您需要在 ValueStack 上放置某种类似 java bean 的对象,就像您在上面所做的那样。
回答by samarjit samanta
Hi just for information
您好,仅供参考
These is a downside of using getValueStack().getContext()
sometimes the data was not available in .ftl (data was not showing in line2, but it was coming in line1) i really dont know the reason for this. But using .getValueStack().set("resDTO",resDTO);
the problem was solved (data was getting populated for both the functions).
这是getValueStack().getContext()
有时使用 .ftl 中的数据不可用的缺点(数据未显示在第 2 行中,但它出现在第 1 行中)我真的不知道这是什么原因。但是使用.getValueStack().set("resDTO",resDTO);
问题已经解决了(两个函数的数据都被填充了)。
${resDTO.data} //line 1
var selonload='<@s.property value="resDTO.data" escape="false" />'; //line 2
回答by Anil Punia
just define a property like
只需定义一个属性,如
String string1 = "test string";
in your action.
在你的行动中。
in jsp you can access directly.
在jsp中可以直接访问。
e.g
例如
<s:property value="string1"/>
will print out
会打印出来
"test string"
“测试字符串”