tags:

views:

39

answers:

3

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.

A: 

edit: Tried it myself and it really seems to be the wrong type. How did you fix it ?

kukudas
Haven't fixed it yet. Roland only gave me the answer as to why it doesn't work.
jeff
+1  A: 

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
You are right, I'm expecting an Integer but it is a long dn=1 dnplus3=4 class=java.lang.Long.
jeff
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
+1  A: 

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