Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call Java code from JavaScript code in Wicket?

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

like image 646
acostache Avatar asked Oct 02 '08 15:10

acostache


People also ask

Can Java interact with JavaScript?

LiveConnect is a technique that allows Java and JavaScript to communicate with each other. It allows your Java class to call JavaScript methods and access the JavaScript environment. JavaScript can also access Java objects and invoke methods on them.

How do you call a Java class in HTML?

Convert your Java Class into Servlet and using Ajax hit the servlet and do the required tasks. look into this link for a starter. Show activity on this post. You can either use a servet engine like tomcat, and write a servlet that will serve your JSON document, or use the com.


2 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

like image 109
Antony Stubbs Avatar answered Oct 19 '22 03:10

Antony Stubbs


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");
like image 33
tetsuo Avatar answered Oct 19 '22 04:10

tetsuo