質問

I have a struts2 form. In this form the user is asked to fill several fields. 2 of those fields are filled by picking an object from another action.

In fact : main form => pickup button => new action with a new form to fill => return to the main form and filling the corresponding field.

My problem is that after the user finished the subaction, I want to return to the main form but not lose everything he filled before ...

I don't know how to handle this, should I pass to the subaction every field from the main form ?

役に立ちましたか?

解決

Several ways come to mind, depending on factors will determine the best one for you.

1) Send out all the parameters you want to keep each time. Easiest way to do this is use hidden fields. Model driven may also make sense here or placing several actions into one class, either way it highlights that those actions share a common set of properties.

2) Store the value in the session like Vasily recommended however Struts2 has the scope interceptor... If you need to do this often it is worth looking at (see last example on page): http://struts.apache.org/2.3.1.2/docs/scope-interceptor.html however to do this right is a little tricky, because if this is an office application and the users are anything like me they we'll have a dozen windows open at any given time. In this situation you'll want to consider also adding a token (see: struts2 token tag) to to each "flow" which will prevent one of a dozen windows when doing a refresh to pick up the latest data from what I would expect rather than, get polluted from some global store that all actions use. There are some other ways to handle flows/conversations... for instance someone wrote a plugin which I suppose would implement what was just explained: http://code.google.com/p/struts2-conversation/

It's always worth a quick look over the struts2 plugins to make sure you aren't reinventing the wheel, although I must say I can't vouch for that plugin.

3) Don't have them leave the page at all. This is very slick... If you need to select additional parameters, when they click on the "pickup" button have the form expand with the required fields then when they click "update" in this subsection, it is close and update the rest of the fields. You will need a combination of JS on the client side with generally an XML or JSON response. For your own pages JSON is the easier way to go, to produce JSON responses see the struts2-json-plugin.

I would go with #3 in most cases. Possibly #1 if I knew the action would only be used from the calling action and/or I wanted a very bookmark safe form(pass all parameters if possible with GET). #2 in more complicated scenarios where multiple actions needed to collaborate and I could not use ajax for some strange reason. Note #3 and #2 are not very book mark friendly, and they are in general not very state friendly without using client side storage.

他のヒント

Store your mainform in session.

public class Test extends ActionSupport implements Preparable {

  private MainForm form;

  public void prepare(){
    form =     (MainForm)ServletActionContext.getRequest().getSession().getAttribute("mainForm");
    if(form == null) {
      form = new MainForm();
    }
  }

  public String execute(){
    //Do something useful
    //Do something useful
    //Do something useful
    return SUCCESS;
  }

  public MainForm getForm() {
    return form;
  }

  public void setForm(MainForm form) {
    this.form = form;
  }

"prepare" method is a part of Preparable interfeice. It will be executed before all parameters readings and before "execute" method every time when your call the acton.

You need the following to have a working struts2 platform:

  1. 2 struts form classes with fields
  2. A struts action class
  3. A jsp page
  4. A struts config file

Then go through the below process:

  • When the first form is loaded save its bean in the second form through a field
  • After you return from the first form load its fields in the second form through the above field
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top