If I can do this, how do I call Java code (methods for instance) from within JavaScript code, in Wicket.
4 Answers
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
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");
-
1Worth mentioning that the API for extracting parameters has changed in Wicket 1.5. Having called
RequestCycle.get().getRequest()
to obtain aRequest
object, you must now callgetQueryParameters()
(for GET params),getPostParameters()
(for POST params), orgetRequestParameters()
(for all at once). Each returns anIRequestParameters
instance that you can query for parameters. See the API docs. Commented Mar 13, 2012 at 19:56 -
Oh, and a gotcha: the URL returned by
getCallbackUrl()
can change during the component construction cycle. It's often inaccurate if you try to use it in the constructor, and callbacks will fail as a result. I've had success using it in an override of the component'sonBeforeRender()
instead. Commented Mar 13, 2012 at 19:59
http://www.wicket-library.com/wicket-examples-6.0.x/index.html/ has plenty of examples to get you going.
Or have a Have a look at DWR
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