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?
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:
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.
Use a JSF component instead of <c:forEach> tag, such as <ui:repeat>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With