ui:repeat不适用于Map [英] ui:repeat doesn't work with Map

查看:139
本文介绍了ui:repeat不适用于Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个键/值的Map,我在@PostConstruct中对其进行了如下初始化:

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;
}

当我尝试使用<ui:repeat>遍历此Map时,如下图所示,并且在Map的getter上设置了一个断点,我注意到它没有被调用,因此什么也没打印:

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>

但是当用<c:foreach>替换上面的代码时,一切正常,并且列表按预期打印,为什么我会出现这种行为?

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?

推荐答案

更新:JSF 2.3(自2017年起)支持此

UPDATE: JSF 2.3 (since 2017) supports this out of the box.

不幸的是,UIDataUIRepeat不支持在JSF中迭代地图.

Unfortunately, UIData and UIRepeat 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

同时,您可以使用一些辅助代码来遍历Map:

In the mean time, you can iterate over a Map with some little helper code:

/**
 * Converts a Map to a List filled with its entries. This is needed since 
 * very few if any JSF iteration components are able to iterate over a map.
 */
public static <T, S> List<Map.Entry<T, S>> mapToList(Map<T, S> map) {

    if (map == null) {
        return null;
    }

    List<Map.Entry<T, S>> list = new ArrayList<Map.Entry<T, S>>();
    list.addAll(map.entrySet());

    return list;
}

然后在*-taglib.xml文件中定义EL函数,如下所示:

Then define an EL function in a *-taglib.xml file like this:

<namespace>http://example.com/util</namespace> 

<function>
    <function-name>mapToList</function-name>
    <function-class>com.example.SomeClass</function-class>
    <function-signature>java.util.List mapToList(java.util.Map)</function-signature>
</function>

最后像这样在Facelet上使用它:

And finally use it on a Facelet like this:

<html xmlns:util="http://example.com/util">

    <ui:repeat value="#{util:mapToList(someDate)}" var="entry" >
        Key = #{entry.key} Value = #{entry.value} <br/>
    </ui:repeat>

这篇关于ui:repeat不适用于Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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