`
riching
  • 浏览: 261422 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(转)JSTL访问Map的问题,key为Integer或Long

    博客分类:
  • JSTL
阅读更多
参考stackoverflows上的这篇文章 http://stackoverflow.com/questions/924451/jstl-access-a-map-value-by-key

Map<Integer, String> map = new HashMap<Integer, String>();  
map.put(1, "One");  
map.put(2, "Two");  
<c:out value="${map[1]}"/>  
无法取到值。

换成
Map<Long, String> map = new HashMap<Long, String>();  
map.put(1L, "One");  
即正常。

原因是JSTL默认把数字封装成Long类型。

另一个问题:类型转换
Map<Long, String> map = new HashMap<Long, String>();
${map[obj.id]}
obj.id定义为int类型时,无法取到值。需要将obj.id转为Long。
在jsp页面可以这样写
${map[obj.id + 0]}  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics