Wicket 6.2 AbstractDefaultAjaxBehavior getCallbackUrl no longer resolves JS variables

假如想象 提交于 2019-11-29 12:38:24
Thomas

Wicket Ajax has been completely rewritten for Wicket 6. See this page for a detailed description.

In your case, you should use the new AjaxRequestAttributes like that:

@Override
protected void updateAjaxAttributes(final AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);
    attributes.getExtraParameters().put("hiddenvalue", "value");
}

Retrieval of the value from the request still works the same as before.

@Override
protected void respond(AjaxRequestTarget target)
{
    getRequest().getRequestParameters().getParameterValue("hiddenvalue");
}

Another cleaner approach is to use the callback function

        AbstractDefaultAjaxBehavior ajaxBehavior = new AbstractDefaultAjaxBehavior() {

        @Override
        protected void respond(AjaxRequestTarget target) {
            String param1Value = getRequest().getRequestParameters().getParameterValue(AJAX_PARAM1_NAME).toString();
            String param2Value = getRequest().getRequestParameters().getParameterValue(AJAX_PARAM2_NAME).toString();
            System.out.println("Param 1:" + param1Value + "Param 2:" + param2Value);
        }

        @Override
        public void renderHead(Component component, IHeaderResponse response) {
            super.renderHead(component, response);
            String callBackScript = getCallbackFunction(CallbackParameter.explicit(AJAX_PARAM1_NAME), CallbackParameter.explicit(AJAX_PARAM2_NAME)).toString();
            callBackScript = "sendToServer="+callBackScript+";";
            response.render(OnDomReadyHeaderItem.forScript(callBackScript));
        }

    };
    add(ajaxBehavior);

Define a variable for the function in your javascript var sendToServer;

It will be initialized on dom ready event by wicket with the callback function

Call sendToServer(x,y) from javascript to pass the parameters to the server.

private static final String MY_PARAM = "myparam";
public static class SampleCallbackBehavior extends AbstractDefaultAjaxBehavior {        
    @Override
    public void renderHead(Component component, IHeaderResponse response) {
        super.renderHead(component, response);
        response.render(OnDomReadyHeaderItem.forScript("var myfunction : " + getCallbackFunction(CallbackParameter.explicit(MY_PARAM))));
    }
    @Override
    protected void respond(AjaxRequestTarget target) {
        StringValue paramValue = getComponent().getRequest().getRequestParameters().getParameterValue(MY_PARAM);
        //TODO handle callback
    }       
}

After this, you should only call the function from javascript

myfunction("paramValue");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!