Master_T Master_T - 8 months ago 65
Java Question

Wicket: run javascript when a Form si submitted

I'm using Wicket 6.xx and I have a Wicket

that I use to upload some files. This is not a problem. The problem is that I need to run some javascript on the page after the form has been submitted in order to show a modal popup that is javascript based.

However, I can't do I because I don't have an
object, since a form submission is not an Ajax call. Or at least, I'm guessing that's the reason. I've tried the following, taken from another thread, but it doesn't work:

From<RequestInfo> uploadFrm = new Form<RequestInfo>("uploadFrm", getModel()) {
private static final long serialVersionUID = 1L;

protected void onSubmit() {
AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
target.appendJavascript("..."); //NullPointerException: target is always null

As you can tell from the comment I put there,
is always

What is the proper way to run some javascript in a situation like this? Is there a proper way?

Answer Source

I think the best way to do that is to add an AjaxSubmitLink to your form so you can call javascript code inside the onSubmit method:

HTML code:

<form wicket:id="uploadFrm">
    <input type="submit" wicket:id="ajaxSubmitLink" value="OK" />

JAVA code:

From<RequestInfo> uploadFrm = new Form<RequestInfo>("uploadFrm", getModel());   
AjaxSubmitLink ajaxSubmitLink = new AjaxSubmitLink("ajaxSubmitLink", uploadFrm) {
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
               target.appendJavaScript("you javascript");