Search code examples
ajaxjsfrichfacescomponentscomposite

JSF: Ajax Rerender composite component


I've read a lot of posts to try and solve my problem but none could help me... So here I am Asking if someone can tell me if I'm doing something wrong:

I have a JSF Composite Component A which I pass a custom attribute value with the ID of another composite component B. Whenever I change something on Component A, Component B should be re-rendered with a4j:ajax function.

This works perfectly.

But, now I need Component B to be initially rendered with an EL expression that depends on some managed bean MB as follows:

<mycomp:B id="comp-b" rendered="#{MB.value gt 0}">

On MB's constructor, values is set to 0. As expected, on first access to page, Component B is not rendered.

Problem:

When selecting some value on component A, it changes MB.value and triggers the ajax valueChange event but the component B won't show up. If I refresh the page or click enter on the Url bar, component B shows up.

I cannot post my actual code here but I will try to show the actual behaviour:

Page.xhtml

<h:form>
  <mycomp:A id="comp-a" changeListener="#{MB.listener}" renderComps="comp-b" />
  <mycomp:B id="comp-b" rendered="#{MB.value gt 0}" />
</h:form>

compA.xhtml

<cc:interface>
  <cc:attribute name="renderComps" />
  <cc:attribute name="listener" method-signature="void m(javax.faces.event.ValueChangeEvent)"  />
</cc:interface>
<cc:implementation>
  <h:outputLabel value="Comp A: " />
  <myComp:select listener="#{cc.attrs.listener}"
                 render="#{cc.attrs.renderComps}"
                 />
</cc:implementation>

compB.xhtml

<cc:interface>
</cc:interface>
<cc:implementation>
 <span id="cc.clientId">
  <h:outputLabel value="Comp B" />
 </span>
</cc:implementation>

I appreciate your help!


Solution

  • You should ensure the parent component of mycomp:B is updated for each ajax requests. Since your component is not rendered initially, you won't be able to reference it for an update, hence the need to update its parent, which will re-evaluate all rendered= expressions

    In your mycomp:select (you didn't give the code), try updating @form, or passing the form id in place of your compB id.

    To figure out which component is updated upon selection, you can observe the XHR response received using a browser debugging tool.