JSF / RichFaces 4问题:渲染时启动组件是错误的

时间:2014-04-07 11:24:14

标签: java jsf-2 richfaces rendering

我有一个隐藏在rendered="false"后面的组件。该组件仍然被初始化并开始一些资源浪费过程。 如何在区域/组件/面板具有rendered="false"时确保组件未初始化?

源代码:

XHTML

<a4j:region rendered="false">
  <h:panelGroup rendered="false"> 
    <rich:dropDownMenu binding="#{textBlockBean.textBlockMenu}" rendered="false" />
  </h:panelGroup>
</a4j:region>

JAVA

public UIComponent getTextBlockMenu() {
  if (textblockMenu == null) {
    textblockMenu = createTextBlok();
  }
  textblockMenuComponent = textblockMenu.build();
  return textblockMenuComponent;
}

我该如何阻止

<rich:dropDownMenu binding="#{textBlockBean.textBlockMenu}"/>

在需要之前被触发。

提前致谢!

2 个答案:

答案 0 :(得分:2)

问题

当EL处理器到达该标签时,决定构建标签绑定的UIComponent实例已经为时已晚,即一旦EL处理器到达<rich:dropDownMenu/>,它会调用getTextBlockMenu()rendered属性是一个视图呈现时间标志,它只是决定是否显示组件。

解决

是否构建组件的决定必须在使用<c:if/> JSTL标记的 view-build 期间进行:

<a4j:region rendered="false">
  <h:panelGroup rendered="false"> 
     <c:if test="#{textBlockBean.renderCondition}">
        <rich:dropDownMenu binding="#{textBlockBean.textBlockMenu}" rendered="false" />
     </c:if>
  </h:panelGroup>
</a4j:region>

其中renderCondition是一个确定组件可用性的支持bean标志

答案 1 :(得分:0)

因为rendered-attribute可能不是静态的,而是EL-expr, 您也可以在getter方法中使用该EL,以防止调用init。

此外,在通话过程中多次调用getter:

Why JSF calls getters multiple times

因此,重新安排编码逻辑,将init-code移出getter方法可能会更好。