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

3

我采用了一种简单的方法,遍历JSF组件树并将组件禁用。(这样用户就无法更改值)。但是这种方法对于复合组件不起作用。我该如何至少检测到复合组件?然后我可以尝试将特殊属性设置为禁用。


你无法检测某个组合组件,因为实现 NamingContainer 接口是组合组件后端 bean 类型的唯一限制,但有很多其他实现该接口的东西。 - rdcrng
请每个问题只提出一个问题。不要改变现有的问题。这会使答案不完整甚至无效。 - BalusC
@BalusC 抱歉,这是我的第一个问题,当我考虑到这个问题时才想到安全性的问题。你能重新回答一下吗?你的回答非常有用。 - Tony
1个回答

4
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"


谢谢你的帮助!但现在我正在考虑安全问题。我编辑了我的问题。 - Tony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接