Struts 会话表单 bean 不保留状态

发布于 2024-10-06 07:12:17 字数 331 浏览 2 评论 0原文

我正在使用 Struts 1.3 创建一个类似向导的界面,其中包含 3 个 jsp 页面和 3 个 Struts 操作。流程如下: 页面1>操作1->页面2>操作2-> page3>action3

我使用会话表单 bean(具有会话范围的操作表单)在请求之间共享数据。我遇到的问题是我在 page2 中提交的数据在操作 2 中可用,但在操作 3 中不可用。我怀疑可能是我在 page3 上没有表单来保存这些数据,或者因为我调用action3 通过 jQuery post 方法而不是常规表单提交,但我真的不确定。

我已经在互联网上挖掘了几乎一天,但仍然没有运气。谁能提供一些帮助。多谢。

I am creating a wizard-like interface consisting of 3 jsp pages and 3 Struts actions using Struts 1.3. The flow is like below:
page1>action1 ->page2>action2 -> page3>action3

I use a session form bean (an action form with session scope) to share data between requests. The problem I am having is that the data I submitted in page2 is available in action 2, but not in action 3. I am in doubt it might be I don't have a form on page3 to hold those data, or because I call action3 via jQuery post method instead of a regular form submit, but I am really not sure.

I have been digging all the internet for almost a day and still no luck. Could anyone offer some help. Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

待"谢繁草 2024-10-13 07:12:17

每个请求都会调用表单上的 Reset() 方法,因此您会丢失状态。您可以通过编程方式控制它。

public class MyForm extends ActionForm {
    boolean reset = true;
    private String[] checkboxes = {};

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        if (reset) {
            this.checkboxes = new String[];
            // etc
        }

        reset = true;
    }

    public void doNotReset() {
        reset = false;
    }
}

让 action2 在表单上调用 doNotReset()。

The reset() method on the form is being called with each request and thus you are losing state. You can programmatically control this.

public class MyForm extends ActionForm {
    boolean reset = true;
    private String[] checkboxes = {};

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        if (reset) {
            this.checkboxes = new String[];
            // etc
        }

        reset = true;
    }

    public void doNotReset() {
        reset = false;
    }
}

Have action2 call doNotReset() on the form.

人间☆小暴躁 2024-10-13 07:12:17

我想您可能已为 StrutsConfig.xml 中的两个操作分配了相同的表单,因此它不会给出 ClassCastException。顺便说一句,如果您想访问在操作 2 中填充的相同表单 bean,请执行以下操作

  1. 查看两个操作的 actionMappingstrutsConfig 文件( 2 和 3)。对于单独的操作,保持不同的表单名称(例如,action2 对应 form2,action3 对应 form3)。
  2. 在 Action3 中,不要强制转换表单,而是使用 form2 = (FormBean2) session.getAttribute("form2");

上面的原因是因为两个操作都使用相同的表单,struts 可能会覆盖它。希望以上能解决您的问题。

I suppose that you might have assigned a same form to both the action in StrutsConfig.xml and hence it is not giving the ClassCastException. By the way, if you want to access the same form bean which was filled on action 2 stuff, do the following

  1. Look at the strutsConfig file for actionMapping of both the actions (2 and 3). keep the name of form different for separate action (e.g. form2 for action2 and form3 for action3).
  2. In Action3, instead of casting the form, use this form2 = (FormBean2) session.getAttribute("form2");

The reason for above is since both the actions are using the same form, struts might have overwriting it. Hopefully above will solve your problem.

一抹苦笑 2024-10-13 07:12:17

感谢您的所有投入。这是我解决问题的方法。我不太喜欢这个解决方案,但这可能是我能找到的最简洁的解决方案。

在第 3 页中,我为我希望在操作 3 中可用的任何属性添加了隐藏字段。Struts 会将值存储在这些隐藏字段中,当再次提交表单时,数据将重新填充到操作表单中。

在我看来,Struts 的工作原理是这样的:当它加载第 3 页时,它会尝试使用 myForm 的值填充第 3 页中的表单。提交表单后,该过程将相反,它会使用用户表单中的值填充 myForm。问题是,在用用户提交的值填充 myForm 之前,它会重置 myForm 的属性。因为重置后,它找不到这些字段的值,所以将其保留为空。

我认为 Struts 那样工作没有意义,但是……就这样吧。

Thank you for all your inputs. Here is how I solved my problem. I don't really like this solution, but it possibly the neatest one I can find.

In page 3 I added hidden fields for what ever property I want to be available in action 3. Struts will store the values in those hidden field and when the form is submitted again, the data will then re-populated to the action form.

It seems to me that Struts works like this: when it loads page 3, it try to populate the form in page 3 with values of myForm. When the form is submitted, the process is reversed, it populate myForm with values from the user's form. The problem is that, before populating myForm with values submitted by user, it resets myForm's properties. And because after reseting, it doesn't find the value for those fields, it leaves it empty.

I don't think it makes sense for Struts to work that way, but... so be it.

余罪 2024-10-13 07:12:17

您如何在action2和action3中访问page2的表单bean。
我想你访问的方式是错误的。您是否收到有关 invalidCast 或其他内容的异常?

How are you accessing the form bean of page2 in action2 as well as in action3.
I suppose you are accessing the wrong way. Are you getting an exception regarding invalidCast or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文