2

in Wicket 1.x I used an AjaxEventBehavior to place a CallBackScript that delivers me the mouse coordinates. This is what I did: (getEventX() and getEventY() are JavaScript Functions)

    myObject.add(new AjaxEventBehavior("onClick") {
        private static final long serialVersionUID = 1L;

        @Override
        protected CharSequence getCallbackScript() {
            return generateCallbackScript("wicketAjaxGet('" + getCallbackUrl()
                    + "&x=' + getEventX(this, event) + '&y=' + getEventY(this, event)");
        }

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            final Request request = MyPage.this.getRequest();
            final IRequestParameters parameters = request.getRequestParameters();
            final int x = Integer.parseInt(parameters.getParameterValue("x").toString("0"));
            final int y = Integer.parseInt(parameters.getParameterValue("y").toString("0"));

That worked quite well. But I don't get how to do this with Wicket 6.x

I do understand, that the way the Ajax link is working was changed. So I tried to use getCallBackUrl in the same way than before. But that did not work.

        public CharSequence getCallbackUrl() {
            final CharSequence callBackUrl = super.getCallbackUrl();

            return callBackUrl + "&x=' + getEventX(this, event) + '&y=' + getEventY(this, event)";
        }

When I take a look at the generated HTML I can see the ajax link looks like this:

Wicket.Ajax.ajax({"u":"../page?5-2.IBehaviorListener.2-cityMap&x=' + getEventX(this, event) + '&y=' + getEventY(this, event)","e":"click","c":"cityMap","i":"id29--ajax-indicator"});;

Looks good, but it does not work.

I am pretty sure, I am doing something wrong since wicket 6 but I dont know how to do it the right way.

Any suggestions are greatly appreciated.

cheers Reinhard

Andreas Krueger
  • 1,497
  • 13
  • 16
Reinhard
  • 31
  • 1
  • 3

3 Answers3

1

I recently posted a howto on Wicket 6 AJAX-Behaviors in my blog which also included passing parameters from javascript to the server:

http://tom.hombergs.de/2013/01/rolling-your-own-ajax-behavior-with.html

Another helpful resource describing the new AJAX features of Wicket 6 is this: https://cwiki.apache.org/WICKET/wicket-ajax.html.

Hope that helps. If not, please describe what kind of error you get when clicking the link (javascript error?).

Regards, Tom

Tom
  • 3,913
  • 19
  • 28
  • Gorky's answer below (http://stackoverflow.com/a/23555363/1118067) is actually a more elegant solution. – Alinoor May 22 '14 at 00:56
1

I think this one is an elegant and straight forward way to go

https://stackoverflow.com/a/23555297/463918

Community
  • 1
  • 1
Gorky
  • 1,393
  • 19
  • 21
0

Try this

public class MyAjaxButton extends AjaxButton {

  @Override
  protected void onEvent(final AjaxRequestTarget target) {
    HttpServletRequest request = (HttpServletRequest) MyAjaxButton.this.getForm().getRequest().getContainerRequest();
    System.out.println("onEvent: " + request.getMethod());
  }

  @Override
  public void renderHead(final Component component, final IHeaderResponse response) {
    super.renderHead(component, response);
    response.render(OnDomReadyHeaderItem.forScript(this.createAjaxCallbackScript()));
  }

  /**
   * Creates a javascript to perform a post callback
   * @return A script to use in renderHead as ajax callback
   */
   private String createAjaxCallbackScript() {
     final String formId = MyAjaxButton.this.getForm().getMarkupId();
     String call = "Wicket.Ajax.ajax({'m':'post', 'f':'" + formId + "', 'u':'" + this.getCallbackUrl() + "', 'e':'click', 'c':'" + MyAjaxButton.this.getMarkupId() + "'})";
    // You need to have "; return false;" to prevent the normal operation of a submit button (by browser)
    return call + "; return false;";
  }

  @Override
  public CharSequence getPreconditionScript() {
    // Note: You need this "return false;" in addition to prevent the additional (GET) ajax request done (by wicket)
    return "return false;";
  }
}

To edit the callback script, see: http://wicket.apache.org/guide/guide/ajax.html#ajax_5

Note: If you do not have the "return false" statements for the scripts, you will receive additional pointless request

Ville Myrskyneva
  • 1,560
  • 3
  • 20
  • 35