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

查看:22
本文介绍了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天全站免登陆