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

4

3 回答 3

1

我认为这是一种优雅而直接的方式

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

于 2014-05-09T01:41:31.023 回答
1

我最近在我的博客中发布了关于 Wicket 6 AJAX-Behaviors 的操作指南,其中还包括将参数从 javascript 传递到服务器:

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

另一个描述 Wicket 6 的新 AJAX 功能的有用资源是:https ://cwiki.apache.org/WICKET/wicket-ajax.html 。

希望有帮助。如果不是,请描述单击链接时遇到的错误类型(javascript 错误?)。

问候,汤姆

于 2013-05-13T14:09:28.907 回答
0

尝试这个

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;";
  }
}

要编辑回调脚本,请参阅: http ://wicket.apache.org/guide/guide/ajax.html#ajax_5

注意:如果您没有脚本的“return false”语句,您将收到额外的无意义请求

于 2014-05-21T13:41:16.273 回答