Iterator JSF value
I want to know how to store a value from data iterator in a JSF page, so that I can use it in multiple place in a single iteration. Lets, say I want to d开发者_如何学Pythonisplay a particular value in 5 different places in a screen.
I'm not sure about the problem you're struggling with. It sounds much like as if you're using JSF on JSP and are using scriptlets for that wherein you're repeating the Iterator#next()
call multiple times within the loop body. This is indeed not going to work. Calling next()
will obviously return the next item. You basically need to get hold of it in a variable only once in the loop and then reuse that variable multiple times. But anyway, using scriptlets in a JSF page is a huge smell.
When using JSP as view technology, the proper way would be using JSTL <c:forEach>
tag or any JSF UIData
component, such as <h:dataTable>
or Tomahawk's <t:dataList>
. All of them offers a var
attribute wherein you can specify the scoped attribute name of the currently iterated item. You can use it as many times as you want in the tag body.
E.g.
<c:forEach items="#{bean.list}" var="item">
#{item} ... #{item} ... #{item}
</c:forEach>
When you're using Facelets as view technology (which -fortunately- doesn't support scriptlets), you should be using the <ui:repeat>
tag for this.
<ui:repeat value="#{bean.list}" var="item">
#{item} ... #{item} ... #{item}
</ui:repeat>
JSF 2.3 (JEE8) supports the Iterable interface in UIData (DataTable) and UIRepeat (Spec 1103) for the first time.
精彩评论