Wicket ajaxeventbehavior causes page refresh in subpanels

1.6k Views Asked by At

I have some troubles with AjaxEventBehaviors on subpanels in my Wicket 1.5 application. What happens is that event behaviors on these subpanels cause page refreshes.

Example: Panel2 is added to Panel1. Every button on Panel1 works perfectly, but not on Panel2 (onEvent is never called, just a page refresh).

Code for event behavior on buttons:

WebMarkupContainer test = new WebMarkupContainer("test");

test.add(new StatelessAjaxEventBehavior("onclick")
    {
        @Override
        protected void onEvent(AjaxRequestTarget target)
        {
            LOG.info("I am clicked...");
            if (callback != null)
            {
                callback.call(target);
            }
        }

        @Override
        protected PageParameters getPageParameters()
        {
            return getPage().getPageParameters();
        }
    });

Does someone know why the page refreshes, and—just as important—how do I stop it?

Thanks in advance

UPDATE
Thank you for your reactions. However, it still doesn't work. To describe the situation better, I added a reproducible path:

  • Add panel to page
  • Add two sub-panels to said panel, called A and B and make A default
  • Add button on main panel and make it replace A for B
  • Add a button to B and make it print 'hello'
  • Press the button and a page refresh will occur without the desired onclick/onevent action.
  • The cause: a button on a stateless page rebuilds the entire page. This means that the default settings will be reloaded. A is thus restored, even before button B is executed. Obviously, this produces errors and within Wicket these errors will result in a page refresh. Placing buttons on stateless sub-panels seems to be impossible.
1

There are 1 best solutions below

0
On

You can use this peace of code in the component which produces the event no matter where it is attached(in your case button)

this.send(getPage(), Broadcast.DEPTH, "onClick");

The page or component which receives the event

public void onEvent(IEvent<?> event) {
    IEventSource source = event.getSource();
    Component sourceComponent = (Component) source;
    String componentId = sourceComponent.getId();
    LOG.debug("Component id is " + componentId);

    if ((componentId == "homeLink") 
            || (componentId == "cancelLink")
            || (componentId == "logoutLink")) 
        LOG.debug("EVENT TRIGERRED...!");
}

Hope this helps you too :)