javascripthtmlajaxwicketwicket-1.6

How to call Java Function from JavaScript Apache Wicket


I have looked in to this topic in many places and found some ways. In this particular scenario, I have used https://cwiki.apache.org/confluence/display/WICKET/Calling+Wicket+from+Javascript article as the reference.

What I did in Java,

public class HomePage extends WebPage {
    private static final long serialVersionUID = 1L;

    public HomePage(final PageParameters parameters) {
        super(parameters);


        final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {
            protected void respond(final AjaxRequestTarget target) {
                target.add(new Label("foo", "Yeah I was just called from Javascript!"));
            }

            public void renderHead(Component component,IHeaderResponse response){

                String componentMarkupId = component.getMarkupId();
                String callbackUrl = getCallbackUrl().toString();

                response.render(JavaScriptHeaderItem.forScript("var componentMarkupId='"+componentMarkupId+"'; var callbackUrl='"+callbackUrl+"';","values"));
            }
        };

        add(behave);

    }

}

and my HomePage.html,

<!DOCTYPE HTML>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function() {
        var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });
        alert(wcall);
    });
</script>
</body>
</html>

What I tried to do is call the get ajax method using the vars I have initialized. But when my page loads, in the firebug console it says,

ReferenceError: Wicket is not defined
[Break On This Error]    

var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '' });

What has gone wrong here ?

Is there any other good way to call Java Function from Javascript?


Solution

  • You need to add the wicket javascripts are added to the page, typically by making sure overriden methods like renderHead make a super.renderHead call.