2

In a servlet I have:

HashMap eventsByDayNo = new HashMap();
eventsByDayNo.put (new Integer(12), "day 12 info");
eventsByDayNo.put (new Integer(11), "day 11 info");
eventsByDayNo.put (new Integer(15), "day 15 info");
eventsByDayNo.put (new Integer(16), "day 16 info");

request.setAttribute("eventsByDayNo", eventsByDayNo);
request.setAttribute("daysInMonth", new Integer(31));

And in a jsp I have:

<c:forEach var="dn" begin="1" end="${daysInMonth}" step="1" varStatus="status">
  Day Number=<c:out value="${dn}" /> Value=<c:out value="${eventsByDayNo[dn]}" /><br>
</c:forEach>

The above JSTL works fine, but if I try to offset the day number <c:out value="${eventsByDayNo[dn+3]}" /> none of the hashmap entries are not printed. Any answers as to why not?

The above is just a proof of concept for my real application.

jeff
  • 3,618
  • 9
  • 48
  • 101

2 Answers2

3

Numbers (at least, whole numbers) in EL are implicitly treated as Long. So replace your Map<Integer, String> by a Map<Long, String> and it will work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

My guess is that dn+3 has type java.lang.Double, not java.lang.Integer (which you may expect).

<ul>
<c:forEach var="dn" begin="1" end="${daysInMonth}" step="1">
  <li>
    <c:set var="dnplus3" value="${dn+3}" />
    dn=<c:out value="${dn}" />
    dnplus3=<c:out value="${dnplus3}" />
    class=<c:out value="${dnplus3.class.name}" />
  </li>
</c:forEach>
</ul>
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • You are right, I'm expecting an Integer but it is a long dn=1 dnplus3=4 class=java.lang.Long. – jeff Oct 12 '10 at 21:35
  • For the reference, the *Expression Language Specification Version 2.2* defines in section 1.7.1 the operator `+`, which in this case results in a `Long` value. – Roland Illig Oct 13 '10 at 22:01
  • Just thinking that this is an example of strong typing fighting back, instead of preventing errors as it was supposed to do, is not doing what the user expects. – stivlo Jun 10 '11 at 13:01