Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jstl

jsf-2

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?

like image 563
user1050204 Avatar asked Nov 16 '11 17:11

user1050204


1 Answers

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:

  • JSTL in JSF2 Facelets... makes sense?
like image 97
BalusC Avatar answered Nov 21 '22 19:11

BalusC