What is the easiest way to do background loading in Wicket?

I have a simple Wicket form that allows users to select some data and then upload a zip file (generated on the fly) containing what they ask. Currently, the form method onSubmit()looks something like this:

public void onSubmit() {
    IResourceStream stream = /* assemble the data they asked for ... */ ;
    ResourceStreamRequestTarget target = new ResourceStreamRequestTarget(stream);
    target.setFileName("download.zip");
    RequestCycle.get().setRequestTarget(target);
}

This works, but of course the request stops there and it is not possible to display any other feedback for the user.

What I would like to have is something like a typical "Your requested [NAME] download should start automatically. If not, click this link." Ideally, the same page is still displayed, so the user can immediately select some other data and load it.

I assume that this can be done using the Wicket Ajax classes, but I managed to avoid using them so far, and it does not immediately seem to me right away. What is my fastest way out here?


Updated for the answer from Zeratul, below: I got something like this:

class MyDownloader extends AbstractAjaxBehavior {

    private final MyForm form;

    MyDownloader(MyForm form) {
        this.form = form;
    }

    void startDownload(AjaxRequestTarget target) {
        target.addComponent(myForm);
        target.appendJavascript("window.location.href='" + getCallbackUrl() + "'");
    }

    @Override
    public void onRequest() {
        try {
            ResourceStreamRequestTarget streamTarget = form.getStreamTarget();
            form.info(/* some status message */);
            getComponent().getRequestCycle().setRequestTarget(streamTarget);
        catch (SomeException e) {
            form.error(e.getMessage());
        }
    }
}

class MyForm extends Form {

    private final MyDownloader myDownloader;
    private final Object myModel;

    MyForm(Object aModel) {
        super("myForm");
        myModel = aModel;
        myDownloader = new MyDownloader(this);

        add(myDownloader);

        add(/* form components */);
        add(new AjaxButton("download", new Model<String>("Download"), this) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                myDownloader.startDownload(target);
            }
        });

        add(new FeedbackPanel("feedback"));
    }

    ResourceStreamRequestTarget getStreamTarget() throws SomeException {
        return /* target based on form input */;
    }
}

This is a little annoying, but it seems to work.

+3
source share
1 answer

This article has an article about Apache clicks, it might suit you:

download ajax

+3
source

Source: https://habr.com/ru/post/1741789/


All Articles