Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 524
Sandeep Avatar asked Dec 28 '22 04:12

Sandeep


2 Answers

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.

like image 121
Paul Croarkin Avatar answered Jan 08 '23 21:01

Paul Croarkin


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.

like image 39
Naved Avatar answered Jan 08 '23 21:01

Naved