JSTL c:forEach does not iterate through a collection

泄露秘密 提交于 2020-01-04 02:07:14

问题


Does anyone has any idea, why this code would work:

<h:outputText value="#{allocations[0].policyNumber}" />

and this code would work:

<c:forEach var="i" begin="1" end="5">
    <h:outputText value="aaa"/>
</c:forEach>

But this code would NOT work (nothing appears in GUI):

<c:forEach var="allocation" items="#{allocations}" >
    <h:outputText value="aaa"/>
</c:forEach>

I am using namespace "http://java.sun.com/jstl/core". allocations is a collection in java. I have tried everything I could think of and have no idea what to try next.


回答1:


The #{} notation is from unified EL. This is only supported in JSTL 1.2. Your JSTL namespace URI in combination with the fact that you're using JSF indicates that you're actually using JSTL 1.1.

You have 2 options:

  1. Use ${} notation instead. You should only guarantee that the bean is already present in the scope. The ${} notation namely won't auto-create managed beans (the #{} does that).

  2. Upgrade to JSTL 1.2. Download links and details can be found in our JSTL wiki page. Don't forget to change the XML namespace URI to http://java.sun.com/jsp/jstl/core.

Needless to say that option 2 is preferred. You should ban ${} from your JSF pages.

See also:

  • Difference between JSP EL, JSF EL and Unified EL

As a completely different alternative, you could also just use Facelets' own <ui:repeat> instead of the <c:forEach>. You should however understand the major difference that the Facelets one runs during view render time and that the JSTL one runs during view build time. See also JSTL in JSF2 Facelets... makes sense?



来源:https://stackoverflow.com/questions/13197016/jstl-cforeach-does-not-iterate-through-a-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!