0

I have the following array:

List<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();

[{name=BIRTH_DATE}, {catVal=07.11.2011}, {catStat=162}, {catVal=30.04.2011}, {catStat=108}, {CatVal=26.01.2011}]

I'd like to use JSTL to choose between name, catVal and CatStat. I've tried the following, but it doesn't work. How can I get the key?

<table border="1">
<c:forEach items="${xml}" var="map">
    <tr>
        <c:choose>
            <c:when test="${map.key =='name'}">
                <td>${map.name}</td> 
            </c:when>
            <c:when test="${map.key == 'catVal'}">
                <td>${map.catVal}</td> 
            </c:when>
            <c:when test="${map.key == 'catStat'}">
                <td>${map.catStat}</td> 
            </c:when>
        </c:choose>

    </tr>
</c:forEach>

George Sharvadze
  • 560
  • 9
  • 25

1 Answers1

0

Assuming your ${xml} contains the array attribute of request/session:

  • You're trying to read the entries of the Maps directly. You should start reading the elements from the list that are Maps.
  • From How to iterate an ArrayList inside a HashMap using JSTL?, you're using the wrong syntax to access the current entry values of your map. You should use entry.value in your code:

    <table border="1">
        <c:forEach items="${xml}" var="itemMap">
            <c:forEach items="${itemMap}" var="mapEntry">
            <tr>
                <td>
                    <c:choose>
                        <c:when test="${mapEntry.key == 'name'}">
                            <c:out value="Name:" />
                        </c:when>
                        <c:when test="${mapEntry.key == 'catVal'}">
                            <c:out value="Cat value:" />
                        </c:when>
                        <c:when test="${mapEntry.key == 'catStat'}">
                            <c:out value="Cat status:" /> 
                        </c:when>
                    </c:choose>
                </td>
                <td>
                    ${mapEntry.value}
                </td>
            </tr>
            </c:forEach>
        </c:forEach>
    </table>
    
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thank you for а comprehensive explanation! – George Sharvadze Mar 31 '13 at 21:48
  • @LuiggiMendoza I also have similar question [here](http://stackoverflow.com/questions/22293665/how-to-show-multiple-tables-depending-on-key-in-the-map-using-jstl). See if you can help me out.. Any help will be appreciated. –  Mar 10 '14 at 21:46
  • @SSH I read it some hours ago and I upvoted the current posted answer because it solves your problem. – Luiggi Mendoza Mar 10 '14 at 21:47