JSTL c:forEach不会遍历集合 [英] JSTL c:forEach does not iterate through a collection

查看:251
本文介绍了JSTL c:forEach不会遍历集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道这个代码为什么起作用:

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

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

,此代码将起作用:

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

但是此代码不起作用(GUI中什么都没有出现):

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

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

我正在使用命名空间"http://java.sun.com/jstl/core".分配是Java中的集合.我已经尝试了所有我能想到的,并且不知道下一步该怎么做.

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.

推荐答案

#{}表示法来自统一的EL.仅在JSTL 1.2中支持.您的JSTL名称空间URI加上您正在使用JSF的事实,表明您实际上在使用JSTL 1.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.

您有2个选择:

  1. 改为使用${}表示法.您只应确保该bean已存在于作用域中. ${}表示不会自动创建托管bean(#{}会这样做).

  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).

升级到JSTL 1.2.下载链接和详细信息可以在我们的JSTL Wiki页面中找到.不要忘记将XML名称空间URI更改为http://java.sun.com/jsp/jstl/core.

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.

不用说,选项2是首选.您应该在JSF页面中禁止${}.

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

作为完全不同的替代方法,您也可以只使用Facelets自己的<ui:repeat>而不是<c:forEach>.但是,您应该了解Facelets在视图渲染期间运行和JSTL在视图构建期间运行的主要区别.另请参见 JSF2 Facelets中的JSTL ...有意义吗?

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?

这篇关于JSTL c:forEach不会遍历集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆