开发者

Struts session form bean doesn't retain state

开发者 https://www.devze.com 2023-01-29 07:17 出处:网络
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:

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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号