3

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?

3
  • did you try it on dom ready? Commented Jul 3, 2013 at 19:49
  • Wicket generates dynamic HTML-pages serverside. Your example is a static HTML page. This is not the concept. Commented Jul 3, 2013 at 19:52
  • This is a test code. False was missing super call in renderHead() Commented Jul 3, 2013 at 19:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Wicket 6.x: in class extending AbstractDefaultAjaxBehavior

@Override
public void renderHead(Component component, IHeaderResponse response) {
    String callbackUrl = getCallbackUrl().toString();

    StringBuffer script = new StringBuffer();
    script
            .append("var callbackUrl = '").append(callbackUrl).append("';\n")
            .append("someJavaScriptCode();\n")
            .append("Wicket.Ajax.get({u: callbackUrl});");

    response.render(OnDomReadyHeaderItem.forScript(script.toString()));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.