代码之家  ›  专栏  ›  技术社区  ›  jeff

JSTL和hashmap不工作

  •  2
  • jeff  · 技术社区  · 15 年前

    在一个servlet中,我有:

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

    在jsp中,我有:

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

    <c:out value="${eventsByDayNo[dn+3]}" /> 没有一个hashmap条目未被打印。为什么不呢?

    以上只是我实际应用的概念证明。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Roland Illig    15 年前

    我猜是的 dn+3 java.lang.Double ,不是 java.lang.Integer (你可能会想到)。

    <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>
    
        2
  •  3
  •   BalusC    15 年前

    EL中的数字(至少是整数)被隐式地视为 Long . 所以把你的 Map<Integer, String> 由 Map<Long, String>