2

Recently I have been working on upgrading a big web application that was using wicket 1.4.18 to 6.2. We had a situation where we would create javascript variables to keep track of positioning within a drag and drop list. This is just the wicket side of the code since the js has always worked and has not been changed.

ListItem.add(new AbstractDefaultAjaxBehavior()
{
    private static final long serialVersionUID = 1L;

    @Override
    public void onComponentTag(ComponentTag tag)
    {
        tag.put("ondrop", "var value = $(ui.item[0]).attr('hiddenvalue');"
            + this.getCallbackScript());
    }


    @Override
    public final CharSequence getCallbackUrl()
    {
        return super.getCallbackUrl() + "&hiddenvalue' + value + '";
    }
}

However the problem I am running into is the javascript variables are not resolving to values and are now being taken as literal strings (Ex: 'value' instead of 5) in the getCallbackUrl. This was not the case in wicket 1.4.18 and I don't believe this problem originated in our migration to 1.5.8.

In the end we just want to be able to pull the value out using

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

Any advice on this? I hope I have provided enough information. Thanks in advance for any help. Some of this is a little beyond my knowledge and can be intimidating not knowing where to look.

Artem Shafranov
  • 2,655
  • 19
  • 19
Jared Sol
  • 313
  • 1
  • 6
  • 20

3 Answers3

4

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");
}
A. Tapper
  • 1,261
  • 8
  • 17
Thomas
  • 2,231
  • 1
  • 19
  • 27
  • When doing what you said using attributes.getExtraParameters().put("hiddenvalue", "value"); I am still getting the string "value" and not the js resolved $(ui.item[0]).attr('hiddenvalue'); value. – Jared Sol Nov 02 '12 at 22:10
  • 4
    Then you should take a look at AjaxRequestAttributes.getDynamicExtraParameters(). These can by arbitrary JS functions and expressions that are evaluated on the client side. – Thomas Nov 03 '12 at 11:22
  • Sorry this is so late but AjaxRequestAttributes.getDynamicExtraParameters() did work for me – Jared Sol Jun 24 '14 at 18:50
3

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.

Gorky
  • 1,393
  • 19
  • 21
1
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");
Golyo
  • 387
  • 4
  • 3