59

I'm trying to ajax-update a conditionally rendered component.

<h:form>
    ...
    <h:commandButton value="Login" action="#{login.submit}">
        <f:ajax execute="@form" render=":text" />
    </h:commandButton>
</h:form>
<h:outputText id="text" value="You're logged in!" rendered="#{not empty user}" />

However, that does not work. I can assure that #{user} is actually available. How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Artem Moskalev
  • 5,748
  • 11
  • 36
  • 55

1 Answers1

108

It's not possible to re-render (update) a component by ajax if the component itself is not rendered in first place. The component must be always rendered before ajax can re-render it. Ajax is using JavaScript document.getElementById() to find the component which needs to be updated. But if JSF hasn't rendered the component in first place, then JavaScript can't find anything to update.

The solution is to simply reference a parent component which is always rendered.

<h:form>
    ...
    <h:commandButton ...>
        <f:ajax ... render=":text" />
    </h:commandButton>
</h:form>
<h:panelGroup id="text">
    <h:outputText ... rendered="#{not empty user}" />
</h:panelGroup>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Shouldn't the bean be at least ViewScoped, or better SessionScoped? – perissf Feb 09 '13 at 21:18
  • But the container does not print any Exception trace. Ususally when the id is not found - it prints that such an id was not found in the scope. – Artem Moskalev Feb 09 '13 at 21:26
  • 1
    Well, then the login is simply invalid :) – BalusC Feb 09 '13 at 21:29
  • That would be a perfect explanation :) But the persistence provider finds it - i checked the code) – Artem Moskalev Feb 09 '13 at 21:30
  • Thanks. But in the colon was troublesome for me. Just entering the id between the quotes worked . – Shoaib Jul 24 '17 at 13:33
  • 1
    @TREMOR: The colon is the correct solution for the code in its current form. You've apparently a different construct whereby everything is placed inside the same naming container component. Check last "See also" link for explanation how to determine the correct client ID. – BalusC Jul 24 '17 at 13:51
  • @BalusC Thank you very much for pointing it out. Just read the the second link. Im new to this hence found it really helpful. In my case it was yes, within the same namingContainer, h:form. – Shoaib Jul 25 '17 at 04:57