简体   繁体   English

JSF 1.2 - 迭代包含集合的Map

[英]JSF 1.2 - iterate over a Map that contains Collections

Using JSF 1.2 and JSP .... 使用JSF 1.2JSP ....

Is it possible to iterate over a Map whose values contain Collections? 是否可以迭代其值包含集合的Map?

I have a Map that looks like this: 我有一个看起来像这样的地图:

Map<String, List<Foo>> myMap;

I would like to iterate over myMap and draw a separate table for each key. 我想迭代myMap并为每个键绘制一个单独的表。

Each table will contaim multiple rows. 每个表都会包含多行。

Each row will represent a Foo object from the ArrayList mapped to the current key. 每行将表示映射到当前键的ArrayList中的Foo对象。

Sadly we are using JSF 1.2 and JSP. 遗憾的是,我们正在使用JSF 1.2和JSP。

I was hoping I could use a nested <h:dataTable> tag, but I'm not having any success. 我希望我可以使用嵌套的<h:dataTable>标签,但我没有任何成功。


Edit : 编辑

Here is my current JSP code after consulting BalusC's answer: 在咨询BalusC的答案之后,这是我当前的JSP代码:

                    <c:forEach items="#{someModule$someBean.prefMap}" var="mapEntry">
                        <br/><br/><p>Key: <h:outputText value="#{mapEntry.key}"/></p>
                        <h:dataTable value="#{mapEntry.value}" var="pref">
                            <h:column><h:outputText value="#{pref.defaultFieldLabel}"/></h:column>
                            <h:column><h:outputText value="#{pref.fieldLabel}"/></h:column>
                        </h:dataTable>
                    </c:forEach>

It causes the following exception: 它会导致以下异常:

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>


Here is some code from my Managed Bean. 这是我的Managed Bean的一些代码。

Note that I'm using HashMap and ArrayList instead of Map and List 请注意,我使用HashMap和ArrayList而不是Map和List

(I read somewhere you couldn't use interfaces, which also don't work) (我读到某处你无法使用接口,这也不起作用)

private HashMap<String, ArrayList<Foo>> prefMap;

public HashMap<String, ArrayList<Foo>> getPrefMap()
{
  if (prefMap == null)
  {
    buildPrefMap();
  }
  return prefMap;
}

private void buildPrefMap()
{
  prefMap = new HashMap<String, ArrayList<Foo>>();
  for (Foo mdp : getFooArray())
  {
    String cat = mdp.getField().getCategory();
    if (! prefMap.containsKey(cat))
    {
      ArrayList<Foo> mpl = new ArrayList<Foo>();
      mpl.add(mdp);
      prefMap.put(cat, mpl);
    }
    else
    {
      prefMap.get(cat).add(mdp);
    }
  }
}

private void dumpMapInfo()
{
  StringBuilder sb = new StringBuilder();
  Map<String, ArrayList<Foo>> theMap = getPrefMap();
  for (String key : theMap.keySet())
  {
    sb.append(key + ": " + theMap.get(key).size());
  }
  System.out.println("\n\n" + sb.toString());
}

Calling dumpMapInfo before rendering the page confirms that the Map is not null and is populated as expected. 在呈现页面之前调用dumpMapInfo确认Map不为null并按预期填充。

JSF doesn't have any component which can iterate over a Map . JSF没有任何可以迭代Map组件。 Only the JSTL <c:forEach> can iterate over a Map . 只有JSTL <c:forEach>可以迭代Map Each iteration gives you a Map.Entry back which in turn has getKey() and getValue() methods. 每次迭代都会为您提供一个Map.Entry ,后者又有getKey()getValue()方法。 You can in turn use a <h:dataTable> to iterate over the map value. 您可以依次使用<h:dataTable>来迭代地图值。

<c:forEach items="#{bean.map}" var="entry">
    <p>Key: <h:outputText value="#{entry.key}" /></p>
    <h:dataTable value="#{entry.value}" var="foo">
        <h:column><h:outputText value="#{foo.prop1}" /></h:column>
        <h:column><h:outputText value="#{foo.prop2}" /></h:column>
        <h:column><h:outputText value="#{foo.prop3}" /></h:column>
    </h:dataTable>
</c:forEach>

Update this works only when you're using JSTL 1.2. 只有在使用JSTL 1.2时才更新此功能。 This fails in JSTL 1.1 because #{} is not recognized in JSTL 1.1 tags and hence you're forced to use ${} , but this in turn fails in the nested JSF components because they accept #{} only. 这在JSTL 1.1中失败,因为在JSTL 1.1标记中无法识别#{} ,因此您被迫使用${} ,但这反过来在嵌套的JSF组件中失败,因为它们只接受#{} You'd basically need to fall back to "plain" JSP/HTML in that entire piece of code, or, better, grab Tomahawk's <t:dataList> . 你基本上需要在整段代码中回归“普通”的JSP / HTML,或者更好的是,抓住Tomahawk的<t:dataList>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM