views:

3149

answers:

4

If I can do this, how do I call Java code (methods for instance) from within JavaScript code, in Wicket.

A: 

Assuming you mean JavaScript running on the client - you cause an HTTP redirect to be made to the server, and have your servlet react to the request for the given URL.

This is known as Ajax, and there are a number of libraries that help you do it..

David Dorward
+3  A: 

http://wicketstuff.org/wicket13/ajax/ has plenty of examples to get you going.

Or have a Have a look at DWR

http://directwebremoting.org/

DWR allows Javascript in a browser to interact with Java on a server and helps you manipulate web pages with the results.

As Dorward mentioned this is done via AJAX

Paul Whelan
+4  A: 

erk. The correct answer would be ajax call backs. You can either manually code the js to hook into the wicket js, or you can setup the callbacks from wicket components in java. For example, from AjaxLazyLoadPanel:

        component.add( new AbstractDefaultAjaxBehavior() {

        @Override
        protected void respond(AjaxRequestTarget target) {
            // your code here
        }

        @Override
        public void renderHead(IHeaderResponse response) {
            super.renderHead( response );
            response.renderOnDomReadyJavascript( getCallbackScript().toString() );
        }

        }

This example shows how to add call back code to any Component in Wicket. After the OnDomReady event fires in your browser, when loading a page, Wicket will cause it's js enging, to call back into your code, using Ajax, to the 'respond' method shown above, at which point you can execute Java code on the server, and potentially add components to the ajax target to be re-rendered.

To do it manually, from js, you can hook into wicket's system by printing out getCallbackScript().toString() to a attribute on a wicket component, which you'll then be able to access from js. Calling this url from js manually with wicket's wicketAjaxGet from wicket-ajax.js.

Check out the mailing list for lot's of conversation on this topic: http://www.nabble.com/Wicket-and-javascript-ts24336438.html#a24336438

Antony Stubbs
+2  A: 

Excerpt from https://cwiki.apache.org/WICKET/calling-wicket-from-javascript.html

If you add any class that extends AbstractDefaultAjaxBehavior to your page, wicket-ajax.js will be added to the header ofyour web page. wicket-ajax.js provides you with two basic methods to call your component:

function wicketAjaxGet(url, successHandler, failureHandler, precondition, channel)

and

function wicketAjaxPost(url, body, successHandler, failureHandler, precondition, channel)

Here is an example:

JavaScript

function callWicket() {
   var wcall = wicketAjaxGet('$url$' + '$args$', function() { }, function() { });
}

$url$ is obtained from the method abstractDefaultAjaxBehavior.getCallbackUrl(). If you paste the String returned from that method into your browser, you'll invoke the respond method, the same applies for the javascript method.

You can optionally add arguments by appending these to the URL string. They take the form &foo=bar.

you get the optional arguments in the Java response method like this:

Map map = ((WebRequestCycle) RequestCycle.get()).getRequest().getParameterMap();

or this:

String paramFoo = RequestCycle.get().getRequest().getParameter("foo");
tetsuo