Search code examples
wicketwicket-6wicket-1.6

How to get AjaxRequestTarget from inside AbstractAjaxBehavior?


I'm in Wicket 6 (client won't approve upgrade, we've asked, it's a whole thing).

I have an AbstractAjaxBehavior that I wish to modify the page from. Normally, I'd drop the components to update into the AjaxRequestTarget. Easy-peasy. But that target isn't available as a parameter to the onRequest(). I have seen suggestions that it can be acquired using:

AjaxRequestTarget target = getRequestCycle().find(AjaxRequestTarget.class);

But this returns null to me. I even saw a recommendation that one could do this to ensure that AjaxRequestTarget would be created.

            getRequestCycle().scheduleRequestHandlerAfterCurrent(new IRequestHandler() {
                @Override
                public void respond(IRequestCycle requestCycle) {
                    AjaxRequestTarget target = getRequestCycle().find(AjaxRequestTarget.class);

But it's still null for me after I do that. I am invoking the behavior thusly:

Wicket.Ajax.get({
    u: ajaxUrl,
    ep: {
        operation: operation, 
        fromItemNo: fromItemNo, 
        toItemNo: toItemNo 
    }
}); 

Which is what I've seen recommended. The parameters do arrive correctly, and control does pass into the behavior. But target is null. Which is a problem because my operation may alter some data that I need to redisplay to the user.

Should I be using a different behavior? A different javascript invocation? A different means of getting the target?


Solution

  • Should I be using a different behavior?

    Yes! You should use AbstractDefaultAjaxBehavior or any of its specializations if you want to use AjaxRequestTarget.

    AbstractAjaxBehavior is intended for use with manual/3rd-party Ajax transport.