Categories

Duncan Mills

Syndicate this blog

JSF and the forEach loop

In feedback for one of my entries on the PetClinic JSF conversion the point was raised about replacements for <c:forEach> in JSF. In most circumstances the various types of DataTable component are fine, the one from the Sun RI for instance. However, these do have an important limitation, they are based around the html <table> tag and so can
only dynamically generate rows.
There are some circumstances where, based on a collection of some sort, you want to generate dynamic columns rather than rows. A tabset menu across the top of a page might be a good example of this, or simply data which you want to represent horizontally, such as the list of specialities in the PetClinic case.
The Sun RI for faces does not have any way of doing this although other components sets do. The ADF Faces component set has a direct replacement for forEach that works pretty well, much like the JSTL version although it had some limitations if the collections are dynamic within the lifetime of the component tree, and it didn't support varStatus in the version I was using. I notice that in the current EA17 version of ADF Faces there is a new component <af:iterator> which is supposed to address many of these issues (and in fact in that version varStatus is now there for forEach as well).
<af:iterator> improves on <af:forEach> in that it can be based on a JSF DataModel and more importantly it can be nested within a table (ie. can use the var attribute EL from a surrounding dataTable.
Paul Cantrell points out that the <x:dataList> component from MyFaces can be used as well, this has some extra chrome options to output the list in various ways, e.g. horizontally, as a numbered list etc.

Comments:

Comment from: Faisal Taimoor [Visitor] · http://ftaimoor.multiply.com
Duncan this is a good article but still you cannot stick to the EA versions but jsf's more focus is over UI not logic. But still a good article to read!
Permalink 22/09/06 @ 13:50
Comment from: Vineet [Visitor]
Hi Duncan,

Thanks a lot.
Your blogs and comments really help

Regards,
Vineet
Permalink 08/11/06 @ 17:58
Comment from: Michael Fons [Visitor] · http://mfonsadf.blogspot.com/
Duncan,

Thanks very much for this blog entry. It is still paying dividends for you apparently, because it just helped me out a lot. I was trying to use af:forEach with a custom component I made. This custom component is a bit like the declarative components you have created in ADF Faces Rich Client components, in that it renders different components depending on what type you send it. I was trying to pass a value binding through an attributes and then trying to evaluate the value of this in the component at encode time, but the af:forEach request scope "var" variable could not be evaluated by the property resolver. af:iterator, I am happy to say, for some reason did something different however so the "var" was more accessable to the child custom component. There was another project also where af:forEach really was acting up, where I wish I had known about af:iterator.

At least I know now. Thanks again!
Permalink 08/07/09 @ 21:51

Comments are closed for this post.