【问题标题】:JSTL and hashmap not workingJSTL 和 hashmap 不起作用
【发布时间】:2010-10-12 21:06:16
【问题描述】:

在我的 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>

上面的 JSTL 工作正常,但如果我尝试抵消日期编号&lt;c:out value="${eventsByDayNo[dn+3]}" /&gt; 没有不打印任何哈希图条目。关于为什么不的任何答案?

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

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    EL 中的数字(至少是整数)被隐式视为Long。所以用Map&lt;Long, String&gt; 替换你的Map&lt;Integer, String&gt; 就可以了。

    【讨论】:

      【解决方案2】:

      我的猜测是 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>
      

      【讨论】:

      • 你说得对,我期待一个整数,但它很长 dn=1 dnplus3=4 class=java.lang.Long。
      • 作为参考,表达式语言规范版本 2.2 在第 1.7.1 节中定义了运算符 +,在这种情况下会产生 Long 值。
      • 仅仅认为这是一个强类型反击的例子,而不是像它应该做的那样防止错误,这并没有达到用户的期望。
      猜你喜欢
      • 1970-01-01
      • 2015-05-15
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多