【问题标题】:Ajax render only if element existsAjax 仅在元素存在时渲染
【发布时间】:2015-10-27 00:28:20
【问题描述】:

我有以下用例:我在屏幕上有一个按钮,用于重新呈现其他元素。

<h:commandButton value="button">
  <f:ajax  execute="@this" render="toBeRendered" />
</h:commandButton>
......
<h:outputPanel id="toBeRendered"/>

我的问题是这个其他元素(toBeRendered)并不总是存在,因为它在页面上的 ui 组合中,所以 JSF 在构建视图时失败并且找不到 f 的 render 属性中的元素:ajax 标签。我知道在较新版本的 mojarra 中不再存在此验证,但更新不是一种选择。有没有人有一种解决方法,只有在其他元素存在时才会呈现?

【问题讨论】:

    标签: ajax jsf jsf-2


    【解决方案1】:

    使用UIComponent#findComponent() 找到它,然后打印它的 ID。如果找不到,无论如何都会考虑null

    <h:commandButton ...>
        <f:ajax ... render="#{component.findComponent('toBeRendered').id}" />
    </h:commandButton>
    ...
    <h:panelGroup id="toBeRendered" />
    

    请注意,#{component} 是一个隐式 EL 对象,它引用当前的 UIComponent 实例。所以代码是完整的。


    与具体问题无关,应注意 Mojarra 2.2.5+ 中缺少的&lt;f:ajax&gt; 验证将在稍后根据issue 1372 重新出现。这让许多初学者感到困惑。

    【讨论】:

    • 不能。问题是按钮位于外部“xhtml”中,而要呈现的元素位于该页面上的 ui 组合中。
    • 您应该将其包含在问题中。我更新了答案。
    • 是的,抱歉问题不正确,我已更新,感谢您的帮助!
    猜你喜欢
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多