ui:repeat 不适用于 Map
我有一个键/值的 Map
,我在 @PostConstruct
中初始化它,如下所示:
Map<String, String> myMap;
@PostConstruct
public void init() {
myMap=new LinkedHashMap<String, String>();
myMap.put("myKey","myValue");
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
当我尝试使用
如下所示,我在 Map 的 getter 上设置了一个断点,我注意到它没有被调用,因此没有打印任何内容:
<ice:panelGroup>
<ui:repeat items="#{myBean.myMap}" var="entry" varStatus="loop">
<input type="checkbox" name="myCheckBoxes" value="#{entry.value}" />
<span class="#{fn:contains(entry.value,'g') ? 'bold-style' : ''}">#{entry.key}</span>
</ui:repeat>
</ice:panelGroup>
但是当用
,一切工作正常,并且列表按预期打印,您知道为什么我会出现这种行为吗?
I have a Map
of key / values, which I initialize in @PostConstruct
as follows:
Map<String, String> myMap;
@PostConstruct
public void init() {
myMap=new LinkedHashMap<String, String>();
myMap.put("myKey","myValue");
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
When I try to iterate over this Map with <ui:repeat>
like shown bellow, and I set a break point on the getter for the Map, I notice that it is not getting called, and so nothing is printed:
<ice:panelGroup>
<ui:repeat items="#{myBean.myMap}" var="entry" varStatus="loop">
<input type="checkbox" name="myCheckBoxes" value="#{entry.value}" />
<span class="#{fn:contains(entry.value,'g') ? 'bold-style' : ''}">#{entry.key}</span>
</ui:repeat>
</ice:panelGroup>
But when replacing above code with <c:foreach>
, everything works fine, and the list is printed as expected, any ideas why I am getting such behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新: JSF 2.3(自 2017 年起)支持此 开箱即用。
不幸的是,
UIData
和UIRepeat
不支持在 JSF 中迭代映射。如果这让您感到困扰(我想确实如此),请为以下问题投票,如果可能的话,留下评论来解释您对此的感受:
http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-479
同时,你可以迭代在带有一些小帮助程序代码的 Map 上:
然后在
*-taglib.xml
文件中定义一个 EL 函数,如下所示:最后在 Facelet 上使用它,如下所示:
UPDATE: JSF 2.3 (since 2017) supports this out of the box.
Unfortunately,
UIData
andUIRepeat
have no support for iterating over a map in JSF.If this bothers you (I guess it does), please vote for the following issue and if possible leave a comment that explains how you feel about this:
http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-479
In the mean time, you can iterate over a Map with some little helper code:
Then define an EL function in a
*-taglib.xml
file like this:And finally use it on a Facelet like this:
也可与
一起使用also use with
<ui:repeat>
似乎在 JSF 1.2 上对我有用,希望它能帮助别人......
Seems to work for me on JSF 1.2, hope it helps someone...
通过 el 2.2 支持,您可以像下面这样迭代地图。
with el 2.2 support you can iterate maps like below.