简体   繁体   English

Struts会话表单bean不保留状态

[英]Struts session form bean doesn't retain state

I am creating a wizard-like interface consisting of 3 jsp pages and 3 Struts actions using Struts 1.3. 我正在使用Struts 1.3创建一个类似向导的界面,包括3个jsp页面和3个Struts动作。 The flow is like below: page1>action1 ->page2>action2 -> page3>action3 流程如下:page1> action1 - > page2> action2 - > page3> action3

I use a session form bean (an action form with session scope) to share data between requests. 我使用会话表单bean(具有会话范围的操作表单)在请求之间共享数据。 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. 我遇到的问题是我在第2页中提交的数据在操作2中可用,但在行动3中没有。我有疑问可能是我在第3页上没有表格来保存这些数据,或者因为我打电话action3通过jQuery post方法而不是常规表单提交,但我真的不确定。

I have been digging all the internet for almost a day and still no luck. 我差不多一天都在挖掘所有互联网,但仍然没有运气。 Could anyone offer some help. 谁能提供一些帮助。 Thanks a lot. 非常感谢。

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. 我想您可能已经为StrutsConfig.xml中的操作分配了相同的表单,因此它没有给出ClassCastException。 By the way, if you want to access the same form bean which was filled on action 2 stuff, do the following 顺便说一句,如果你想访问在action 2中填充的同一个表单bean,请执行以下操作

  1. Look at the strutsConfig file for actionMapping of both the actions (2 and 3). 查看strutsConfig文件以获取actionMapping的两个动作(2和3)。 keep the name of form different for separate action (eg form2 for action2 and form3 for action3). 保持表单的名称不同以进行单独的操作(例如,action2的form2和action3的form3)。
  2. In Action3, instead of casting the form, use this form2 = (FormBean2) session.getAttribute("form2"); 在Action3中,使用此form2 = (FormBean2) session.getAttribute("form2");而不是强制转换表单form2 = (FormBean2) session.getAttribute("form2");

The reason for above is since both the actions are using the same form, struts might have overwriting it. 上面的原因是因为两个动作都使用相同的形式,struts可能会覆盖它。 Hopefully above will solve your problem. 希望以上将解决您的问题。

The reset() method on the form is being called with each request and thus you are losing state. 每个请求都会调用表单上的reset()方法,因此您将丢失状态。 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. 让action2在表单上调用doNotReset()。

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. 在第3页中,我添加了隐藏字段,用于我希望在操作中可用的属性3.Struts会将值存储在这些隐藏字段中,当再次提交表单时,数据将重新填充到操作表单中。

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. 在我看来,Struts的工作原理如下:当它加载第3页时,它会尝试使用myForm的值填充第3页中的表单。 When the form is submitted, the process is reversed, it populate myForm with values from the user's form. 提交表单时,该过程相反,它使用用户表单中的值填充myForm。 The problem is that, before populating myForm with values submitted by user, it resets myForm's properties. 问题是,在使用用户提交的值填充myForm之前,它会重置myForm的属性。 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. 我不认为Struts以这种方式工作是有意义的,但是......就这样吧。

How are you accessing the form bean of page2 in action2 as well as in action3. 如何在action2和action3中访问page2的表单bean。
I suppose you are accessing the wrong way. 我想你是以错误的方式访问的。 Are you getting an exception regarding invalidCast or something. 您是否收到有关invalidCast或其他内容的例外情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM