I have a form which, onSubmit, requests some input-dependent data from a server and creates a file and a ResourceStream of it.
The file is rather important, so the download should start immediately. But I can't allow multiple requests/submits to happen, so I want to switch the page or replace the panel at the same time.
Is there a way to do this? Download the file and switch page/panel in a single click?
So far, I create a ResourceStreamRequestHandler handler and use getRequestCycle().scheduleRequestHandlerAfterCurrent(handler) to initiate the download during onSubmit.
protected void onSubmit() {
// Get the data.
// ...
ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(
getResourceStream(data...), getFileName(data...));
getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
//oldPanel.this.replaceWith(new newPanel(data...));
//setResponsePage(mainPage.class);
}
The problem is, if I use setResponsePage to return to the main page, the download does not start, and if I try to replaceWith(new newPanel()), the download starts, but the panel is not properly replaced. I even tried to initiate the download from newPanel (e.g. during onAfterRender, etc.), but the panels still weren't swapped correctly.
Thanks to martin-g pointing me in the right direction, I finally figured it out.
Using
org.apache.wicket.extensions.ajax.AjaxDownloadBehavioris the way to go, but martin-g's answer lacks some crucial information:AjaxDownloadBehaviorcannot be added "on the fly", as it relies on JavaScript being incorporated into the header on page/panel load. So it should be added in the constructor.AjaxDownloadBehaviordoes not operate on models and neither it nor theIResource/ResourceReferenceobjects it takes as parameters (as far as I could see) allow for dynamic changes to the download resource, one must create a customIResourceorResourceReferenceclass for that.An example to demonstrate the idea:
Still, I'll have to abandon this approach, as the form validation, as I would expect and prefer it (e.g. tooltips popping up, directing the user to required fields), doesn't seem to work for me with Ajax components.