【问题标题】:How to find composite component in JSF component tree?如何在 JSF 组件树中找到复合组件?
【发布时间】:2013-08-16 09:19:08
【问题描述】:

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

【问题讨论】:

  • 您无法检测某个东西是否是复合的,因为复合组件支持 bean 类型的唯一限制是实现 NamingContainer,但还有很多其他东西实现了该接口。
  • 请对每个问题提出一个问题。不要改变现有的问题。它使答案不完整甚至无效。
  • @BalusC 对不起,这是我的第一个问题,当我想到这个问题时,关于安全的想法就出现了。你能重新创建你的答案吗?你的回答很有用。

标签: jsf-2 composite-component


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2011-06-27
  • 2023-04-03
  • 1970-01-01
  • 2013-03-23
  • 2013-04-22
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
相关资源
最近更新 更多