0

I have a form with an inputfield, a label and an ajax button. I want to change the value of the label based on the ajax request AND i want the textfield to remember entered value via browser autocomplete. The basic structure is based on browser native autocomplete for dynamically generated forms (handled with ajax)

I reached both goals and the form works fine, both when submitting by Enter and by Button. I can even change the textfield value and submit again. Only if I submit the Form a second time without changing the textfield value, I get a org.apache.wicket.core.request.mapper.StalePageException. It does not matter whether the first or the second submit happened through Enter or Button. Any combination leads to a stale renderCount as long as the textfield value is not changed. And 'not changed' includes reentering the same value again. That leads to the Exception, too.

I am not quite sure how entering a new value in a textfield changes the way wicket processes the page. Any help welcome, please.

HTML:

    <html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
      <head>
        <wicket:head>
          <title>Suche   </title>
        </wicket:head>
      </head>
      <body>
        <form  target="the_iframe" wicket:id="w_form" id="myform" >
          <input wicket:id="w_input"/> 
          <label wicket:id="w_label">   </label>  
          <input wicket:id="w_button" type="submit"  onclick="myform.submit();"/>
        </form>
        <iframe name="the_iframe" hidden="true">   </iframe> 
      </body>
    </html>

Javacode:

public class TestseitePage extends WebPage {

  private final Form w_form = new Form("w_form");

  @Override
  protected void onInitialize() {
    super.onInitialize();
    final TextField<String> w_input = new TextField<String>("w_input", new Model<String>(""));
    final Model m = new Model<String>("Test");
    final Label w_label = new Label("w_label", m);
    w_label.setOutputMarkupId(true);
    add(w_form);
    w_form.add(w_label);
    w_form.add(w_input);
    final AjaxButton w_button = new AjaxButton("w_button") {
      @Override
      protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
        try {
          Thread.sleep(2000);
        } catch (final InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        m.setObject("Button " + new Date().getTime());
        target.add(w_label);
      }
    };
    w_form.add(w_button);
  }
}

1 Answers1

1

Remove onclick="myform.submit();" from the button's markup. It looks to me that this will trigger non-Ajax submit of the Form and this will lead to full page re-render and thus the change of the Page#renderCount

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • If I remove that, the browser no longer remembers, which values were already inserted into the textfield (browser autocomplete). – Georg Datterl Oct 20 '17 at 12:56
  • 1
    The browser remembers only when there is form submit, yes. The browser doesn't know how different frameworks like Wicket do Ajax form submit, so it cannot plug there. You will have to apply hacks like https://stackoverflow.com/a/27979191/497381 (custom form action pointing to iframe) to achieve this. Or just use non-Ajax submit in your app if this is an option. – martin-g Oct 20 '17 at 13:13
  • non-Ajax submit is not an option, because I need to update parts of the page. I do have the hidden iframe as target of the form. My button submits the form and executes its ajax code. I don't quite see the difference to https://stackoverflow.com/a/27979191/497381 except for the second button who triggers the form submit. – Georg Datterl Oct 20 '17 at 13:38
  • Even with the second button the problem remains: A second submit without changing the textfield value results in a `org.apache.wicket.core.request.mapper.StalePageException`. – Georg Datterl Oct 25 '17 at 07:31