Java EL 通过整数键访问映射值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/924451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EL access a map value by Integer key
提问by Steve Kuo
I have a Map keyed by Integer. Using EL, how can I access a value by its key?
我有一个由整数键控的地图。使用 EL,如何通过键访问值?
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
I thought this would work but it doesn't (where map is already in the request's attributes):
我认为这会起作用,但它不起作用(其中 map 已经在请求的属性中):
<c:out value="${map[1]}"/>
Follow up:I tracked down the problem. Apparently ${name[1]}
does a map lookup with the number as a Long
. I figured this out when I changed HashMap
to TreeMap
and received the error:
跟进:我找到了问题所在。显然${name[1]}
用数字作为一个地图查找Long
。当我更改HashMap
为TreeMap
并收到错误时,我发现了这一点:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
If I change my map to be:
如果我将地图更改为:
Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");
then ${name[1]}
returns "One". What's with that? Why does <c:out>
treat a number as a long. Seems counterintuitive to me (as int is more commonly used than long).
然后${name[1]}
返回“一”。那是怎么回事?为什么<c:out>
将数字视为长。对我来说似乎违反直觉(因为 int 比 long 更常用)。
So my new question is, is there a EL notation to access a map by an Integer
value?
所以我的新问题是,是否有 EL 表示法可以通过Integer
值访问地图?
采纳答案by VonC
Initial answer (EL 2.1, May 2009)
初步答案(EL 2.1,2009 年 5 月)
As mentioned in this java forum thread:
正如这个java论坛线程中提到的:
Basically autoboxing puts an Integer object into the Map. ie:
基本上自动装箱将一个 Integer 对象放入 Map 中。IE:
map.put(new Integer(0), "myValue")
EL (Expressions Languages) evaluates 0 as a Long and thus goes looking for a Long as the key in the map. ie it evaluates:
EL(表达式语言)将 0 计算为 Long,因此会寻找 Long 作为映射中的键。即它评估:
map.get(new Long(0))
As a Long
is never equal to an Integer
object, it does not find the entry in the map.
That's it in a nutshell.
由于 aLong
永远不等于Integer
对象,因此它不会在映射中找到条目。
简而言之就是这样。
Update since May 2009 (EL 2.2)
自 2009 年 5 月以来的更新 (EL 2.2)
Dec 2009 saw the introduction of EL 2.2 with JSP 2.2 / Java EE 6, with a few differences compared to EL 2.1.
It seems ("EL Expression parsing integer as long") that:
2009 年 12 月推出了带有 JSP 2.2/Java EE 6 的 EL 2.2,与EL 2.1 相比有一些差异。
似乎(“ EL 表达式解析整数为长”):
you can call the method
intValue
on theLong
object self inside EL 2.2:
您可以在 EL 2.2 中调用对象 self
intValue
上的方法Long
:
<c:out value="${map[(1).intValue()]}"/>
That could be a good workaround here (also mentioned below in Tobias Liefke's answer)
这可能是一个很好的解决方法(也在下面Tobias Liefke的回答中提到)
Original answer:
原答案:
EL uses the following wrappers:
EL 使用以下包装器:
Terms Description Type
null null value. -
123 int value. java.lang.Long
123.00 real value. java.lang.Double
"string" ou 'string' string. java.lang.String
true or false boolean. java.lang.Boolean
JSP page demonstrating this:
演示此内容的 JSP 页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.*" %>
<h2> Server Info</h2>
Server info = <%= application.getServerInfo() %> <br>
Servlet engine version = <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
Java version = <%= System.getProperty("java.vm.version") %><br>
<%
Map map = new LinkedHashMap();
map.put("2", "String(2)");
map.put(new Integer(2), "Integer(2)");
map.put(new Long(2), "Long(2)");
map.put(42, "AutoBoxedNumber");
pageContext.setAttribute("myMap", map);
Integer lifeInteger = new Integer(42);
Long lifeLong = new Long(42);
%>
<h3>Looking up map in JSTL - integer vs long </h3>
This page demonstrates how JSTL maps interact with different types used for keys in a map.
Specifically the issue relates to autoboxing by java using map.put(1, "MyValue") and attempting to display it as ${myMap[1]}
The map "myMap" consists of four entries with different keys: A String, an Integer, a Long and an entry put there by AutoBoxing Java 5 feature.
<table border="1">
<tr><th>Key</th><th>value</th><th>Key Class</th></tr>
<c:forEach var="entry" items="${myMap}" varStatus="status">
<tr>
<td>${entry.key}</td>
<td>${entry.value}</td>
<td>${entry.key.class}</td>
</tr>
</c:forEach>
</table>
<h4> Accessing the map</h4>
Evaluating: ${"${myMap['2']}"} = <c:out value="${myMap['2']}"/><br>
Evaluating: ${"${myMap[2]}"} = <c:out value="${myMap[2]}"/><br>
Evaluating: ${"${myMap[42]}"} = <c:out value="${myMap[42]}"/><br>
<p>
As you can see, the EL Expression for the literal number retrieves the value against the java.lang.Long entry in the map.
Attempting to access the entry created by autoboxing fails because a Long is never equal to an Integer
<p>
lifeInteger = <%= lifeInteger %><br/>
lifeLong = <%= lifeLong %><br/>
lifeInteger.equals(lifeLong) : <%= lifeInteger.equals(lifeLong) %> <br>
回答by Dave
Just another helpful hint in addition to the above comment would be when you have a string value contained in some variable such as a request parameter. In this case, passing this in will also result in JSTL keying the value of say "1" as a sting and as such no match being found in a Map hashmap.
除了上述注释之外,另一个有用的提示是当您在某些变量(例如请求参数)中包含字符串值时。在这种情况下,传入它也会导致 JSTL 将 say "1" 的值作为字符串进行键控,因此在 Map 哈希图中找不到匹配项。
One way to get around this is to do something like this.
解决这个问题的一种方法是做这样的事情。
<c:set var="longKey" value="${param.selectedIndex + 0}"/>
This will now be treated as a Long object and then has a chance to match an object when it is contained withing the map Map or whatever.
现在这将被视为一个 Long 对象,然后有机会匹配包含在地图 Map 或其他内容中的对象。
Then, continue as usual with something like
然后,像往常一样继续
${map[longKey]}
回答by Dhanashri
Based on the above post i tried this and this worked fine I wanted to use the value of Map B as keys for Map A:
基于上面的帖子,我尝试了这个,并且效果很好,我想使用 Map B 的值作为 Map A 的键:
<c:if test="${not empty activityCodeMap and not empty activityDescMap}">
<c:forEach var="valueMap" items="${auditMap}">
<tr>
<td class="activity_white"><c:out value="${activityCodeMap[valueMap.value.activityCode]}"/></td>
<td class="activity_white"><c:out value="${activityDescMap[valueMap.value.activityDescCode]}"/></td>
<td class="activity_white">${valueMap.value.dateTime}</td>
</tr>
</c:forEach>
</c:if>
回答by Jasper de Vries
If you just happen to have a Map
with Integer
keys you cannot change, you could write a custom EL functionto convert a Long
to Integer
. This would allow you to do something like:
如果您碰巧有一个无法更改的Map
withInteger
键,您可以编写一个自定义 EL 函数将 a 转换Long
为Integer
. 这将允许您执行以下操作:
<c:out value="${map[myLib:longToInteger(1)]}"/>
回答by Tobias Liefke
You can use all functions from Long, if you put the number into "(" ")". That way you can cast the long to an int:
如果将数字放入 "(" ")",则可以使用 Long 中的所有函数。这样你就可以将 long 转换为 int:
<c:out value="${map[(1).intValue()]}"/>