Java FormComponents具有通用的isVisible方法

Java FormComponents具有通用的isVisible方法,java,wicket,Java,Wicket,有没有办法让几个不同的wicket组件具有相同的isVisible()实现 例如,我有标签、文本字段、下拉选项等等,它们都有相同的isVisible方法,但我不习惯为它们实现自定义类,因为很难维护对代码的更改 顺便说一句,由于页面的设计原因,我不能将它们放在webmarkupcontainer中 我希望他们都能继承这样的东西 public class DepositoryFormComponent extends Component { public DepositoryFormComponen

有没有办法让几个不同的wicket组件具有相同的isVisible()实现

例如,我有标签、文本字段、下拉选项等等,它们都有相同的isVisible方法,但我不习惯为它们实现自定义类,因为很难维护对代码的更改

顺便说一句,由于页面的设计原因,我不能将它们放在webmarkupcontainer中

我希望他们都能继承这样的东西

public class DepositoryFormComponent extends Component
{
public DepositoryFormComponent(String id) {
    super(id);
}

public DepositoryFormComponent(String id, IModel model) {
    super(id, model);
}

public boolean isVisible() {
    return isFormDepositoryType();
}

protected boolean isFormDepositoryType() {
    return getCurrentSelections().getSelectedOwnedAccount().getAssetType() == AssetType.DEPOSITORY;
}

protected CurrentSelections getCurrentSelections() {
    return (CurrentSelections) getSession().getAttribute(CurrentSelections.ATTRIBUTE_NAME);
}

public void onRender(){};

}您有几个选择:

  • 如果您已经控制了标记,并且可以在单个标记中分组所有要控制可见性的组件,则可以使用标记使组件控件对整个标记可见。请注意,这不会影响页面设计,并且会达到与添加
    WebMarkupContainer

  • 您可以向这些组件添加一个
    IBehavior
    ,它将计算可见性,并在
    组件上调用
    setVisible()
    。如果不希望以后调用
    setVisible()
    来改变
    组件的可见性,也可以调用。可能不完全像覆盖
    是可见的
    ,但我认为如果不创建自定义组件,就不太可能实现覆盖

    public class VisiblityControlBehavior extends AbstractBehavior { 
    
        private boolean isComponentVisible() { 
            return isFormDepositoryType();
        } 
    
        protected boolean isFormDepositoryType() {
            return getCurrentSelections().getSelectedOwnedAccount().getAssetType() == AssetType.DEPOSITORY;
        }
    
        protected CurrentSelections getCurrentSelections() {
            return (CurrentSelections) getSession().getAttribute(CurrentSelections.ATTRIBUTE_NAME);
        }
    
        @Override 
        public void bind(Component component) { 
            boolean visible = isComponentVisible(); 
            component.setVisible(visible); 
            component.setVisibilityAllowed(visible); 
        } 
    }