【问题标题】:Iterating through an arraylist in a hashmap based on key in JSTL基于 JSTL 中的键迭代哈希图中的数组列表
【发布时间】:2017-11-13 13:48:07
【问题描述】:

我有一个哈希图为Map<String, List<String>> coverageDataMap。 我需要根据一个键遍历它返回的列表。

我是这样做的:

    <c:forEach items="${bean.coverageDataMap['my_key']}"
            var="entry" varStatus="loop">
        <tr>
            <td><h:outputText value="#{loop.index+1}"/></td>
            <td><h:outputText value="#{entry}"/></td>
        </tr>
    </c:forEach>

不幸的是,它不起作用。我环顾四周,但找不到任何东西。请让我知道我在这里做错了什么!谢谢。

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    请尝试使用

    $
    

    如果

    #
    

    【讨论】:

      【解决方案2】:

      使用以下代码运行测试,按要求工作。 检查是否设置了“my_key”。

      test.xhtml

      <html xmlns="http://www.w3.org/1999/xhtml" lang="en"
        xmlns:h="http://xmlns.jcp.org/jsf/html" 
        xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
      <h:head>
          <title>Test</title>
      </h:head>
      <h:body>
         Key is set:<h:outputText value="${bean.coverageDataMap['my_key'] ne null}"/>
         <br/>
         <br/>
          <c:forEach items="${bean.coverageDataMap['my_key']}" 
                     var="entry" varStatus="loop">
              <tr>
                  <td><h:outputText value="#{loop.index+1}"/></td>
                  <td><h:outputText value="#{entry}"/></td>
              </tr>
          </c:forEach>
      </h:body>
      

      TestBean.java

      @ManagedBean(name = "bean")
      @RequestScoped
      public class TestBean implements Serializable{
      
      private static final long serialVersionUID = 1L;
      
      Map<String, List<String>> coverageDataMap;
      
      public TestBean() {
          coverageDataMap = new HashMap<>();
          ArrayList<String> list = new ArrayList<>();
          for (int i = 1; i <= 10; i++) {
              list.add("hello" + i);
          }
          coverageDataMap.put("my_key", list);
      }
      
      public Map<String, List<String>> getCoverageDataMap() {
          return coverageDataMap;
      }
      
      public void setCoverageDataMap(Map<String, List<String>> coverageDataMap) {
          this.coverageDataMap = coverageDataMap;
      }
      }
      

      【讨论】:

      • 由于某种奇怪的原因,它不适用于 标记,但在我使用 c:out 标记时可以正常工作。
      • 似乎是herehere讨论的 JSTL 版本问题
      猜你喜欢
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 2017-09-04
      • 2015-08-30
      相关资源
      最近更新 更多