आनंद
आनंद

Reputation: 2582

Iterating through an arraylist in a hashmap based on key in JSTL

I have a hashmap as Map<String, List<String>> coverageDataMap. I need to iterate through the list returned by it based on a key.

I'm doing it the following way:

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

Unfortunately it doesn't work. I've looked around but couldn't find anything. Please let me know what did I get wrong here! Thanks.

Upvotes: 1

Views: 171

Answers (2)

Shivam Semwal
Shivam Semwal

Reputation: 11

Run a test with following code, working as required. Check if the "my_key" is set.

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

Upvotes: 1

Ivan
Ivan

Reputation: 8758

Please try to use

$

instead if

#

Upvotes: 1

Related Questions