如何在JSF组件树中查找复合组件?

时间:2013-08-16 09:19:08

标签: jsf-2 composite-component

我实现了一个简单的方法,它迭代JSF组件树并将组件设置为禁用。 (因此用户无法更改值)。但是这种方法对复合组件不起作用。我怎样才能至少检测到复合材料成分?然后我可以尝试将特殊属性设置为禁用。

1 个答案:

答案 0 :(得分:2)

UIComponent类有一个isCompositeComponent()辅助方法,用于此目的。

所以,这应该只是:

for (UIComponent child : component.getChildren()) {
    if (UIComponent.isCompositeComponent(child)) {
        // It's a composite child!
    }
}

对于对“幕后”工作感兴趣,这里是Mojarra 2.1.25的实现源代码:

public static boolean isCompositeComponent(UIComponent component) {

    if (component == null) {
        throw new NullPointerException();
    }
    boolean result = false;
    if (null != component.isCompositeComponent) {
        result = component.isCompositeComponent.booleanValue();
    } else {
        result = component.isCompositeComponent =
                (component.getAttributes().containsKey(
                           Resource.COMPONENT_RESOURCE_KEY));
    }
    return result;

}

因此,存在一个名为Resource.COMPONENT_RESOURCE_KEY定义的组件属性,其值为"javax.faces.application.Resource.ComponentResource"

相关问题