多个组件的相同 AjaxEventBehavior

Same AjaxEventBehavior for multiple components

我将图像和文本组件添加到 WebMarkupContainer,如下所述:

filter.add(newFilterLabel("textSub", customerText, filtervalue));
filter.add(newFilterImage("imgSub", filtervalue));

对于每个组件,都有一个 AjaxEventBehavior 做不同的事情。我想以一种独立于单击哪个组件的方式执行相同操作的方式对其进行更改。

private Component newFilterLabel(String id, IModel<String> customText,
    final SourceFilterValue currentValue) {
    final BBLabel label = new BBLabel(id, customText);
    label.add(new AjaxEventBehavior("onclick") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(currentValue,filteredsources, target);
        }
    });
    return label;
}

private Image newFilterImage(String id, final SourceFilterValue filterValue) {
    final Image img = new Image(id, resources.getImage(EXPAND_ICON));
    img.add(new AjaxEventBehavior("onclick") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(img);
        }
    });
    return img;
}

您对如何更改它或任何解决方法有什么建议吗?我使用 Wicket 1.5.8.

AjaxBehavior 只能绑定到单个元素。要么将其添加到层次结构中的父级,要么让两种行为都调用相同的方法:

@Override
protected void onEvent(AjaxRequestTarget target) {
    doSomething(filterValue); //filterValue has to be final to be able to access it from the inner class
}