struts2 - 理解值栈
我有一个关于 struts2 值栈的问题。假设我有一个名为 RegisterAction
的 Action 类,它有一个执行方法,如下所示:
public String execute() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new String("test string"));
return SUCCESS;
}
我的 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>
因此,在该类中执行执行方法后,控制将流向 success.jsp。
我的问题是:
1)如何获取在 success.jsp
中压入堆栈的值?
2) 假设在 success.jsp
中,我有一个
,它重定向到名为 TestAction< 的操作类/代码>。换句话说,在 Register 页面中,用户单击“提交”,然后在
RegisterAction
的执行方法中,我们将“测试字符串”压入堆栈。然后我们转到success.jsp
。 success.jsp
有一个提交按钮,将我们引导至 TestAction#testMethod
。在TestAction#testMethod
中,我在RegisterAction#execute
中压入堆栈的值是否仍然存在?我怎样才能得到它?我单步调试了 eclipse 调试器,但没有看到该值。
谢谢。
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:
public String execute() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new String("test string"));
return SUCCESS;
}
My struts.xml looks like this:
<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.
My questions are:
1) how do I get that value I pushed on the stack in the 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.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我明白了。
1)我发现在值堆栈上获取对象以便我们可以从jsp访问它们的方式是这样的:
换句话说,我们可以将一个HashMap放在包含我们需要的对象的值堆栈上。然后,在jsp中,我们可以像这样访问实际值:
它将查看值堆栈并在我们推送的HashMap中找到这些值。
2)
操作类的一个实例仅与一个请求相关联。因此,当我们转到另一个操作,然后到达另一个 jsp 时,我们从第一个操作推入值堆栈的内容将不会在那里,因为另一个操作有它自己的值堆栈。
参考: http://www.manning-sandbox.com/thread.jspa?messageID =93045
如果其中有任何错误或者有更聪明的方法来完成这些事情,你们可以随时纠正我:)。
谢谢。
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:
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:
It will look through the value stack and find those values in the HashMap we pushed.
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
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.
Struts 2 在执行时将您的操作添加到值堆栈的顶部。因此,将内容放入值堆栈的常用方法是将值的 getter/setter 添加到 Action 类中。您仍然使用 s:property 标记来访问这些值。
CRUD 教程: http://struts.apache.org/ 2.1.6/docs/crud-demo-i.html
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.
A CRUD tutorial: http://struts.apache.org/2.1.6/docs/crud-demo-i.html
定义一个属性即可
只需在您的操作中
。在jsp中可以直接访问。
例如
将打印出
just define a property like
in your action.
in jsp you can access directly.
e.g
will print out
通常,正如 Nate 所说,您将在操作中使用字段,因为操作始终位于 ValueStack 上。但是,如果您正在编写拦截器代码,则这不起作用,因为调用模板(JSP/freemarker 等)时拦截器将消失。在那里,您需要将某种类似 java bean 的对象放在 ValueStack 上,就像上面所做的那样。
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.
您好,仅供参考
这是使用 getValueStack().getContext() 的一个缺点,有时数据在 .ftl 中不可用(数据未显示在第 2 行中,但它出现在第 1 行中)我真的不知道原因。但使用
.getValueStack().set("resDTO",resDTO);
问题得到了解决(两个函数的数据都已填充)。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).