Ajax update/render does not work on a component which has rendered attribute
Asked Answered
I

1

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?

Intemerate answered 9/2, 2013 at 16:14 Comment(0)
F
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:

Fabric answered 9/2, 2013 at 20:4 Comment(7)
Shouldn't the bean be at least ViewScoped, or better SessionScoped?Standoff
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.Intemerate
Well, then the login is simply invalid :)Fabric
That would be a perfect explanation :) But the persistence provider finds it - i checked the code)Intemerate
Thanks. But in <f:ajax ... render=":text" /> the colon was troublesome for me. Just entering the id between the quotes worked <f:ajax ... render="abcd" />.Eslinger
@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.Fabric
@Fabric 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.Eslinger

© 2022 - 2024 — McMap. All rights reserved.