AJAX update and file download in one blowPage edited by Sven Meierstripped down to single solutionFollowing example shows how to use an AJAX request to refresh some components and at the same time make the browser ask back for a file to be downloaded: final AJAXDownload download = new AJAXDownload() { @Override protected IResourceStream getResourceStream() { return createResourceStream(item.getModelObject()); } }; item.add(download); item.add(new AjaxLink<Void>("link") { @Override public void onClick(AjaxRequestTarget target) { // do whatever with the target, e.g. refresh components target.addComponent(...); // finally initiate the download download.initiate(target); } }); We have to add a custom behavior which enables us to initiate the download after the AJAX request has been completed. The overridden hook method provides an IResourceStream (e.g. a FileResourceStream) to serve the actual content. import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.behavior.AbstractAjaxBehavior; import org.apache.wicket.request.target.resource.ResourceStreamRequestTarget; import org.apache.wicket.util.resource.IResourceStream; /** * @author Sven Meier * @author Ernesto Reinaldo Barreiro ([email protected]) */ public abstract class AJAXDownload extends AbstractAjaxBehavior { /** * Call this method to initiate the download. */ public void initiate(AjaxRequestTarget target) { CharSequence url = "" target.appendJavascript("window.location.href=''"); } public void onRequest() { getComponent().getRequestCycle().setRequestTarget( new ResourceStreamRequestTarget(getResourceStream())); } /** * Hook method providing the actual resource stream. */ protected abstract IResourceStream getResourceStream(); }
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Wicket > AJAX update and file download in one ... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
- [CONF] Apache Wicket > AJAX update and file download in... confluence
