Ajax 仅在元素存在时渲染

Ajax render only if element exists

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

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

我的问题是这个其他元素 (toBeRendered) 并不总是存在,因为它在页面上的 ui 组合中,所以当它 builds 视图并且不能'找不到 f:ajax 标记的渲染属性中的元素。我知道这个验证在新版本的 mojarra 中不再存在,但更新不是一个选项。有没有人有解决方法,以便它只在其他元素存在时呈现?

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

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

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


与具体问题无关,请注意,Mojarra 2.2.5+ 中缺少的 <f:ajax> 验证稍后会根据 issue 1372 恢复.这让许多初学者感到困惑。