如何将一个行为添加到Wicket中添加到该组件的另一个行为中的组件中
我想使用ApacheWicket向AjaxEventBehavior中的组件添加AttributeAppender。行为有一个getComponent()方法,但在构造函数中getComponent()显然返回null 现在,我将组件传递给AjaxEventBehavior的构造函数,它正在工作,但这是实现我的目标的好方法吗 以下是我正在做的: AjaxTooltipBehavior:如何将一个行为添加到Wicket中添加到该组件的另一个行为中的组件中,wicket,wicket-1.6,Wicket,Wicket 1.6,我想使用ApacheWicket向AjaxEventBehavior中的组件添加AttributeAppender。行为有一个getComponent()方法,但在构造函数中getComponent()显然返回null 现在,我将组件传递给AjaxEventBehavior的构造函数,它正在工作,但这是实现我的目标的好方法吗 以下是我正在做的: AjaxTooltipBehavior: public class AjaxTooltipBehavior extends AjaxEventBehav
public class AjaxTooltipBehavior extends AjaxEventBehavior {
public AjaxTooltipBehaviour(String event, Component tooltippedComponent) {
super(event);
tooltippedComponent.add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
}
...
}
这就是我使用它的方式:
...
final WebMarkupContainer icon = new WebMarkupContainer("icon"); //a tooltiped icon
icon2.add(new AjaxTooltipBehaviour("mouseover",icon2)
我问自己,如果不将组件传递给AjaxTooltipBehavior,是否有办法将AttributeAppender添加到组件中。
有人知道这在wicket是可能的还是有更好的解决方案?
仅供参考:我正在使用wicket 1.6
提前感谢您的支持!
Ronny因为您已经从AbstractAjaxBehavior(AjaxEventBehavior扩展了AbstractAjaxBehavior)进行了扩展,所以您应该能够访问getComponent(),这将为您提供行为所附加到的组件。通常您会覆盖
行为#onBind(component)
,但是这个方法在AbstractAjaxBehavior
中是最终的。但是它将调用onBind()
,您可以在那里使用getComponent()
:
@Override
protected void onBind() {
super.onBind();
getComponent().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
}
我重写Behavior#onConfigure(Component Component)
这可能是添加行为或对属于行为的组件执行其他操作的最合适方法
@Override
protected void onConfigure(Component component) {
super.onConfigure();
component().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip"));
}
这个解决方案有效,但我找到了一个更好的方法。我覆盖了“onConfigure(Component Component)”,这似乎是使用行为组件的正确位置。不过还是要谢谢你!您绝对不应该将attributeappender添加到onConfigure。这意味着每次重新呈现组件时都会添加属性appender,例如,您将获得data tooltip=“wicketAjaxTooltip wicketAjaxTooltip”。