EL通过整数键获取HashMap的值 [英] EL get value of a HashMap by Integer key

查看:62
本文介绍了EL通过整数键获取HashMap的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HashMap:

I have this HashMap:

    Map<Integer, String> odometerMap = new LinkedHashMap<Integer, String>();
    odometerMap.put(0, getLocaleForKey("drop-down.any"));
    odometerMap.put(1, "< 1000");
    odometerMap.put(2, "1000 - 5000");
    odometerMap.put(3, "5000 - 10000");
    odometerMap.put(4, "10000 - 20000");
    odometerMap.put(5, "20000 - 30000");
    odometerMap.put(6, "30000 - 40000");
    odometerMap.put(7, "40000 - 60000");
    odometerMap.put(8, "60000 - 80000");
    odometerMap.put(9, "> 80000");

我在JSP中的目标是打印$ {odometerMap [2]}(结果为空字符串):

My goal in JSP is to print for example ${odometerMap[2]} (result is empty string):

    <c:out value="${odometerMap[2]}"/>

如果我只打印$ {odometerMap},我会得到完整的地图:

If I print only ${odometerMap} I get the full map:

{0=Any, 1=< 1000, 2=1000 - 5000, 3=5000 - 10000, 4=10000 - 20000, 5=20000 - 30000, 6=30000 - 40000, 7=40000 - 60000, 8=60000 - 80000, 9=> 80000}

如何仅打印我选择的元素?例如:2?

How can I print only an element of my choice? Ex: 2?

谢谢

推荐答案

在EL中,数字被视为Long.它正在寻找Long键.如果您使用Long而不是Integer作为地图键,那么它将起作用.

In EL, numbers are treated as Long. It's looking for a Long key. It'll work if you use Long instead of Integer as map key.

Map<Long, String> odometerMap = new LinkedHashMap<Long, String>();
odometerMap.put(0L, getLocaleForKey("drop-down.any"));
odometerMap.put(1L, "< 1000");
// ...

这篇关于EL通过整数键获取HashMap的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆