【问题标题】:How to check if rich:component returns null如何检查rich:component是否返回null
【发布时间】:2015-08-12 06:26:21
【问题描述】:

我有一个名为 pageContent.xhtml 的 ui:composition,它包含在另一个 ui:composition 和一个 rich:popupPanel(两个不同的页面)中。

pageContent.xhtml 包含一个a4j:commandButton。在按钮的oncomplete 方法中,我只想在rich:popupPanel 上显示pageContent.xhtml 时执行javascript。因此,如果rich:popupPanel 可用,我想检查 pageContent.xhtml。

RichFaces documentation 描述了如果找不到指定的组件标识符,rich:component 将返回 null

出于测试原因,我在a4j:commandButtononcomplete方法中执行如下JS:

console.log('-> ' + #{rich:component('popupPanelId')});

如果 pageContent.xhtml 显示在弹出窗口中(id 'popupPanelId' 可用)并且我单击按钮,我会在控制台上看到以下结果:

-> BaseComponent, PopupPanel

如果 pageContent.xhtml 显示在另一个 ui:composition 页面上(id 'popupPanelId' 不可用)并且我单击按钮,我收到以下错误消息:

try {console.log('-> ' + );;} catch (e) {window.RichFaces.log.error('Error in me...

如果#{rich:component('popupPanelId')} 未找到组件,则不会将任何内容返回到结果页面而不是null

如何检查组件是否可用且没有任何错误?

【问题讨论】:

    标签: javascript jsf richfaces


    【解决方案1】:

    EL表达式在服务器上求值,结果转换成字符串输出到页面,null转换成空字符串。

    所以你可以从空检查开始:

    oncomplete="#{rich:component('popupPanelId') == null ? 'alert(\'not found\')' : 'alert(\'found\')'}"
    

    另一方面,我假设 popupPanel 是基于某些条件包含在内的,更好的方法是让 @oncomplete 检查相同的条件。否则,如果它没有被渲染,函数会找到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-14
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多