struts2 - 理解值栈

发布于 2024-08-13 01:38:26 字数 1594 浏览 6 评论 0原文

我有一个关于 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.jspsuccess.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

櫻之舞 2024-08-20 01:38:27

好吧,我明白了。

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

换句话说,我们可以将一个HashMap放在包含我们需要的对象的值堆栈上。然后,在jsp中,我们可以像这样访问实际值:

<s:property value="key" />
<s:property value="key2" />

它将查看值堆栈并在我们推送的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:

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:

<s:property value="key" />
<s:property value="key2" />

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.

嘴硬脾气大 2024-08-20 01:38:27

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

〆凄凉。 2024-08-20 01:38:27

定义一个属性即可

String string1 = "test string";

只需在您的操作中

。在jsp中可以直接访问。

例如

 <s:property value="string1"/>

将打印出

“测试字符串”

just define a property like

String string1 = "test string";

in your action.

in jsp you can access directly.

e.g

 <s:property value="string1"/>

will print out

"test string"

∝单色的世界 2024-08-20 01:38:27

通常,正如 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.

古镇旧梦 2024-08-20 01:38:27

您好,仅供参考

这是使用 getValueStack().getContext() 的一个缺点,有时数据在 .ftl 中不可用(数据未显示在第 2 行中,但它出现在第 1 行中)我真的不知道原因。但使用 .getValueStack().set("resDTO",resDTO); 问题得到了解决(两个函数的数据都已填充)。

 ${resDTO.data} //line 1
var selonload='<@s.property value="resDTO.data" escape="false" />'; //line 2

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

 ${resDTO.data} //line 1
var selonload='<@s.property value="resDTO.data" escape="false" />'; //line 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文