Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
960 views
in Technique[技术] by (71.8m points)

jsp - EL get value of a HashMap by Integer key

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

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

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

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}

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

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");
// ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...