用jstl从后台传过来的一个map中取值.后台的map是这样写的
Map<Integer, History> historyMap传到页面这样取值:
<c:set var="aPeriod" value="${selectYear*100+selectMonth}" />
${historyMap[aPeriod]}
可以确保map里有数据.
但是为啥这样取值完全取不出来.
更让我抓狂的在下面:<c:forEach items="${historyMap}" var="entry">
  <c:if test="${entry.key == aPeriod}">true</c:if>   <!-- 结果是true -->
  <c:if test="${entry.key eq aPeriod}">true</c:if>   <!-- 结果是true -->
  ${historyMap[entry.key]}                       <!-- 有数据 -->
  ${historyMap[aPeriod]}                      <!-- 为空 -->
</c:forEach>为啥会出现这结果?难道是aPeriod的数据类型与key不同,所以才取不出值?
我把后台的Map<Integer, History> historyMap换成Map<String, History> historyMap.
还是一样的结果..熟悉jstl的帮忙看看,谢谢

解决方案 »

  1.   

    1,将historyMap放在范围中。如:request.setAttribute("historyMap",historyMap); 
    2.页面获取:<c:forEach items="${historyMap}" var="entry"> 
    <c:if test="${entry.key=='aPeriod'}"> 
    <c:out value="${entry.value}"/> 
    </c:if> 
    </c:forEach> 
      

  2.   

    不会吧..aPeriod是变量还套引号?
      

  3.   

    就是放在request里取的.
    不会是这个问题..
      

  4.   

    刚才也试了下,外部的数字没办法提升到Integer类型。标签自己遍历能自动转换成需要的类型
      

  5.   

     ${historyMap[aPeriod]}  <!-- 为空 -->
    路过 顶下
      

  6.   

    总结下..
    可能是el的一个bug吧,页面的int变量在map取值的时候不能自动转化为Integer.
    解决方法是写一个自定义函数.很简单的两行:
    public static Object getMapValue(int key, Map map) {
    return map.get(key);
    }
    本来很简单的事到最后变复杂了.
    希望有更好的办法..
      

  7.   

    Map<Integer, History> historyMap
    改成Map<String, History> historyMap
    string是可以你那样取数据的
      

  8.   

    不行的.
    用el在页面算出来的int数,怎么转成String