ui:repeat 不适用于 Map

发布于 2024-12-21 22:16:37 字数 979 浏览 2 评论 0原文

我有一个键/值的 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 时, 如下所示,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下凄凉 2024-12-28 22:16:38

更新: JSF 2.3(自 2017 年起)支持此 开箱即用

不幸的是,UIDataUIRepeat 不支持在 JSF 中迭代映射。

如果这让您感到困扰(我想确实如此),请为以下问题投票,如果可能的话,留下评论来解释您对此的感受:

http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-479

同时,你可以迭代在带有一些小帮助程序代码的 Map 上:

/**
 * 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 函数,如下所示:

<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 上使用它,如下所示:

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

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

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

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

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

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>

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>
浅唱々樱花落 2024-12-28 22:16:38
<a4j:repeat value="#{myBean.myMap.entrySet().toArray()}" var="_entry">
        <h:outputText value="#{_entry.key}"/><br/>
        <h:outputText value="#{_entry.value}"/>
</a4j:repeat>

也可与 一起使用

<a4j:repeat value="#{myBean.myMap.entrySet().toArray()}" var="_entry">
        <h:outputText value="#{_entry.key}"/><br/>
        <h:outputText value="#{_entry.value}"/>
</a4j:repeat>

also use with <ui:repeat>

时光无声 2024-12-28 22:16:38

似乎在 JSF 1.2 上对我有用,希望它能帮助别人......

    <h:panelGroup>
      <ul>
        <ui:repeat value="#{myBean.myMap.keySet().toArray()}" var="key">
          <li>key:#{key}</li>
          <li>value:#{myBean.myMap[key]}</li>
        </ui:repeat>
      </ul>
    </h:panelGroup>

Seems to work for me on JSF 1.2, hope it helps someone...

    <h:panelGroup>
      <ul>
        <ui:repeat value="#{myBean.myMap.keySet().toArray()}" var="key">
          <li>key:#{key}</li>
          <li>value:#{myBean.myMap[key]}</li>
        </ui:repeat>
      </ul>
    </h:panelGroup>
忱杏 2024-12-28 22:16:38

通过 el 2.2 支持,您可以像下面这样迭代地图。

<ui:repeat value="#{myBean.stats.keySet().toArray()}" var="x">
    <h:outputText value="#{myBean.stats.get(x)}" /><br />
</ui:repeat>

with el 2.2 support you can iterate maps like below.

<ui:repeat value="#{myBean.stats.keySet().toArray()}" var="x">
    <h:outputText value="#{myBean.stats.get(x)}" /><br />
</ui:repeat>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文