Question

I'm having a component hiding behind a rendered="false". The component still gets initialized and starting some resource wasting processes. How to make sure components don't get initialized when their region/component/panel has rendered="false"?

Source code:

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;
}

How can I prevent

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

from getting triggered before needed.

Thanks in advance!

Était-ce utile?

La solution

The Problem

By the time the EL processor gets to that tag, it's already too late to decide to not build the UIComponent instance that the tag is bound to i.e. once the EL processor gets to the <rich:dropDownMenu/>, it's going to call getTextBlockMenu(). The rendered attribute is a view-render time flag that simply decides whether or not to show the component.

To Solve

The decision whether to build the component must be made during view-build using the <c:if/> JSTL tag:

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

Where renderCondition is a backing bean flag that determines the availability of the component

Autres conseils

as the rendered-attribute is probably not static, but an EL-expr, you could use that EL also in your getter-method, to prevent invoking the init.

Furthermore the getter is called several times during the call:

Why JSF calls getters multiple times

so it might be better, to rearrange your coding logic, to move init-code out of a getter method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top