Search code examples
jsf-2jstl

JSF method of f:event preRenderView called after c:forEach


I'm doing a page with jsf 2.0 and i wanna do something like this:

<f:metadata>
    <f:viewParam name="id" value="${id}" />
    <f:event type="preRenderView" listener="#{controller.initPage(id)}"/>
</f:metadata>
....(Some code)....
<c:forEach items="#{bean.listLoadedByInitPage}" var="var">
    #{var.something}
</c:forEach>

The method initPage(id) must load list in the bean. But seems that method is called after than c:forEach loads items not before. Any ideas?


Solution

  • JSTL tags runs during view build time. The <f:event type="preRenderView"> runs right before view render time. In other words, <c:forEach> runs before <f:event>. So, this behaviour is fully expected.

    You have 2 options:

    1. Use @ManagedProperty instead of <f:viewParam>, or when the bean is in the view scope or broader, grab it manually from ExternalContext#getRequestParameterMap() inside @PostConstruct. And, use @PostConstruct instead of <f:event type="preRenderView">. Yes, this makes the entire <f:metadata> obsolete. You can safely remove it.

    2. Use a JSF component instead of <c:forEach> tag, such as <ui:repeat>.

    See also: