Skip to content Skip to sidebar Skip to footer

Call Wicket 6 Code From Javascript And Return Value

I have managed to call my Wicket 6 Java code from Javascript using option A in this example: https://stackoverflow.com/a/42612027/1047418 However, I have not been able to find exam

Solution 1:

I think you can do it in a simpler way!

Wicket Ajax API is just: Wicket.Ajax.ajax({...}). All you need to prepare at the server side is to save the callback url, e.g. by saving it globally in the window object or in HTML element's attributes (data-the-url).

publicclassCallFromJavascriptBehaviorextendsAbstractDefaultAjaxBehavior {
   @Overrideprotectedvoidrespond(AjaxRequestTarget target) {
      finalStringValueparameterValue= RequestCycle.get().getRequest().getQueryParameters().getParameterValue("yourName");
      System.out.println(String.format("Hello %s", parameterValue.toString()));

      // write anything to the WebResponse and then consume it in the JS success handler. See below
   }

   @OverridepublicvoidonComponenntTag(ComponenntTag tag, Component component) {
       super.onComponenntTag(tag, component);
       tag.put("data-the-url", getCallbackUrl());
   }
}

Then in your JS code you can do:

var callbackUrl = jQuery("#theElementId").data("the-url");
Wicket.Ajax.get({"u": callbackUrl, "sh":[successHandler], "fh": [failureHandler] });

Where successHandler and failureHandler are JS functions defined inline (e.g. function(...) {}) or elsewhere.

More documentation you can find at: https://ci.apache.org/projects/wicket/guide/7.x/single.html#_ajax_request_attributes_and_call_listeners

A blog article with an complete example at http://wicketinaction.com/2012/07/wicket-6-javascript-improvements/

Solution 2:

You can just write a Resource and mount it, and get it with your favorite Ajax-approach.

For example:

publicclassMyResourceextendsAbstractResource@Overrideprotected ResourceResponse newResourceResponse( Attributes attributes )
    {

        ResourceResponseresourceResponse=newResourceResponse();
        resourceResponse.setContentType( "text/json" );
        resourceResponse.setTextEncoding( "utf-8" );

        HttpServletRequestrequest= (HttpServletRequest) attributes.getRequest().getContainerRequest();

        try
        {
            this.json = IOUtils.toString( request.getInputStream() );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        resourceResponse.setWriteCallback( newWriteCallback()
        {
            @OverridepublicvoidwriteData( Attributes attributes )throws IOException
            {
                OutputStreamoutputStream= attributes.getResponse().getOutputStream();
                Writerwriter=newOutputStreamWriter( outputStream );


                writer.write( MyResource.this.json );
                writer.close();
            }
        } );

        return resourceResponse;
    }

(Copied from my other answer here https://stackoverflow.com/a/17876029/461499)

And see here for mounting it: https://dzone.com/articles/how-implement-rss-feeds-custom

Post a Comment for "Call Wicket 6 Code From Javascript And Return Value"