forEachは、JSTL<c:forEach>タグの代替タグです。<c:forEach>は、JSF 1.2/JSP 2.1/JSTL 1.2の時点でJSFのコンポーネントまたはタグとともに使用できますが、遅延された評価とともに使用すると、varStatusはサポートされません。このタグはvarStatus(非サポートのcurrent以外)のサポートを強化するものです。(注意: このタグはFaceletsではサポートされていません。これはFaceletsではc:forEachが完全に機能するためです)。ただし、JSF 1.1でビルドされた古いADF af:forEachとは異なり、このタグはJSP 2.1ベースのタグ、JSFまたは非JSFで使用できます。またこのタグには、<c:forEach>、<af:forEach>にはない制限があり、現在、任意のjava.util.Collectionsはサポートされていません。イテレートは、java.util.Listsまたは配列でのみ可能です。
forEachタグを使用するには、目的と知識が必要です。forEachタグは、JSFでのイテレーションに使用されませんが、かわりに複数コンポーネントの生成に使用されます。オブジェクトのコレクションでイテレートし、項目ごとにHTMLをレンダリングすることが目的の場合は、かわりに<af:iterator>を使用してください。
<af:iterator>ではなく<af:forEach>が必要とされる場合の例:
<source> <af:forEach var="item" items="#{bean.views}"> <af:declarativeComponent viewid="O__item_viewId____gt___lt__af_ForEach_gt__________pre___source___li_______li_When__lt_af_iterator__gt__is_not_supported_as_a_child__This_usually_occurs_in_Trinidad__but_the_RichClient_components_______are_written_to_support_iterator__li______ul_____div_style_"padding: 6px;"> <af:forEach> may cause issues with component IDs
There may only be one component in a naming container with an ID. Therefore, if a component that is created as a child of a for each loop has an explicit ID set, JSF will alter the IDs of components beyond the first one. This can break partial triggers, behaviors and other components that refer to a component. Objects in the items of an <af:forEach> tag should not be added, removed or re-ordered once the component tree has been created
For each loops are made to work in JSP, and have been altered to support JSF at a basic level only. The components that are created while looping over the for each loop store their indexes of when the component was first created. This means that if, say for example, the first item is removed from the items collection, the component at index 1, which now is index 0 still retains index 1. As a result, the EL expressions of the component will return the incorrect item when the var is resolved. Problems may also occur if explicit IDs are not given with component state. The component state is tied to the index of the component, not to the item in the items collection. Children of <af:forEach> cannot share a binding EL value
A component instance may only exist once in a component tree. Therefore, if it is desired to have the component bound to a managed bean, the binding must evaluate to a different bean property for every iteration of the loop. For example:
<source> <af:forEach var="item" items="#{bean.items}"> <!-- Note that the output text component is bound to the item, not the bean --> <af:outputText binding="#{item.outputText}" /> </af:ForEach>
</source>
<source> <af:selectOneListbox value="#{someValue}"> <af:forEach var="item" items="#{model.listOfItems}"> <af:selectItem value="#{item.value}" text="#{item.text}"/> </af:forEach> </af:selectOneListbox>
</source>
<source> <af:forEach varStatus="vs" begin="1" end="5"> <af:outputText id="ot2" value="#{vs.index} #{vs.count} #{vs.begin}"/> </af:forEach>
</source>
名前 | 型 | ELのサポート | 説明 |
---|---|---|---|
begin | int | なし | イテレーションを開始する索引。 |
end | int | なし | イテレーションを終了する索引。 |
items | Object | ELのみ | イテレートするコレクション。 |
step | int | なし | 各イテレーションで増分する数値。 |
var | String | なし | イテレート中に公開される変数の名前。 |
varStatus | String | なし | イテレート中に公開されるループ・ステータスの名前。これを介して、index、count、begin、end、step、first、lastプロパティが利用可能になります。 |