1

I have the following code: detailsMap is map of LinkedHashMap type. I am able to get the value ${roleType} as L1, correctly printing the value of ${detailsMap["L1"]} but not ${detailsMap[roleType]}. Please suggest what I am doing wrong. roleTypes are enum values.

<c:set var="detailsMap" value="${hostDetails.value}" />
    <c:forEach items="${roleTypes}" var="roleType">
    <td>${detailsMap[roleType]}</td>
    <td>${detailsMap["L1"]}</td>
    <td><input type="text" name="${roleType}" value="" /></td>
    </c:forEach>
    </tr>
    </c:forEach>

I made it working by setting the attribute as String List in place of enum Values.

PhantomM
  • 825
  • 6
  • 17
  • 34

3 Answers3

1

I would have expected that to work. I have some code here where I do something like this:

<c:set var="roleType" value="${someOtherVariable}" scope="page"/>
<li class="${cssMap[roleType]}"/>

And that works. Does the iteration variable need to be placed in page scope for some reason?

Keith
  • 4,144
  • 1
  • 19
  • 14
  • I need iteration variable only for this iteration. But I am able to get correct value of roleType at . – PhantomM Jun 25 '13 at 17:27
1

If roleType is an enum, you should use .name() method when you use it as a key of a map:

<td>${detailsMap[roleType.name()]}</td>
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Gere
  • 11
  • 2
0

Not sure I understood very well the question, but from what I understood this part :

<c:forEach items="${roleTypes}" var="roleType">
<td>${detailsMap[roleType]}</td>

Should be replaced with something like this :

<c:forEach items="${roleTypes}" var="roleType">
<td>${detailsMap[${roleType}]}</td>

The explanation is that roleType is not a string, but an iterator, what you want is the value of the iterator, therefore you need the ${}.

Coudn't test this, but it's what I thought might be a problem.

Related : How to iterate an Arraylist inside a HashMap using jstl ?.

Best of luck.

Community
  • 1
  • 1
Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • Hum... Is it necessary to iterate over that `enum` ? – Akheloes Jun 25 '13 at 20:23
  • Now, I am passing the String List not enum values then, its working. Thank you for help. ${detailsMap[${roleType}]} expression gives an error. Although I am ble to use ${detailsMap[roleType]} with String list. – PhantomM Jun 25 '13 at 21:17
  • so you made `roleType` a string list ? nice idea. – Akheloes Jun 25 '13 at 22:25
  • Yes, just before setting the attribute, I converted it into a String List. – PhantomM Jun 25 '13 at 22:48
  • Would be nice if you could post the answer for futur generations, this type of posts may be of use for many new developers to come :) – Akheloes Jun 25 '13 at 22:51