Search code examples
javawicketwicket-6

Wicket: addOrReplace() inside Form's onSubmit()


I have a wicket Form inside a page, declared like this:

protected void onBeforeRender() {
    Form<Void> frm = new Form<Void>("frm") {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit() {
                super.onSubmit();
                doSomething();
            }
        };

    //other stuff...
    addOrReplace(frm);
}

doSomething is a method in the page itself. Inside that method I do:

private void doSomething(){
    //stuff...
    addOrReplace(new Label("labelID", "Some text"));
}

The problem is that the call to addOrReplace does not work. I don't see the text added to the page. It only shows up if I refresh.

How can I make calls to addOrReplace work from inside a onSubmit() callback?


Solution

  • Sounds like you're working with AjaxButtons. You'll have to update the component or any of its parents to make the change visible in the browser:

    ajaxRequestTarget.add(frm);
    

    Remember to call #setOutputMarkupId(true) on the component you want to update via Ajax:

    frm.setOutputMarkupId(true);
    addOrReplace(frm);
    

    To get hold of the AjaxRequestTarget you could override AjaxButton#onSubmit(AjaxRequestTarget, Form) and call doSomething from there - passing the AjaxRequestTarget to any method which has to update something.

    Alternatively you can always call #getRequestCycle().find(AjaxRequestTarget.class) to get the current AjaxRequestTarget.