EL通过整数键访问映射值

时间:2022-06-15 17:51:42

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]}对数字做了一个长时间的查找。当我将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]}返回“一”。是什么呢?为什么 把数字当作long。对我来说似乎是违反直觉的(因为int比long更常用)。

So my new question is, is there a EL notation to access a map by an Integer value?

我的新问题是,是否有EL表示法通过一个整数值来访问映射?

5 个解决方案

#1


112  

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:

基本上自动装箱将一个整数对象放入映射中。即:

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,因此查找map中的键值为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.

作为一个Long,永远不等于一个整数对象,它没有在映射中找到条目。简单地说就是这样。


Update since May 2009 (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月,EL 2.2引入JSP 2.2 / Java EE 6,与EL 2.1相比有一些差异。看来(“EL表达式解析整数as long”):

you can call the method intValue on the Long object self inside EL 2.2:

可以在EL 2.2中调用长对象self的方法intValue:

<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>

#2


11  

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 hashmap中无法找到匹配。

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.

这将被视为一个长对象,然后有机会在包含映射映射或其他内容时匹配一个对象。

Then, continue as usual with something like

然后,像往常一样继续

${map[longKey]}

#3


7  

You can use all functions from Long, if you put the number into "(" ")". That way you can cast the long to an int:

如果你把数字填入“(”)”中,你就可以使用所有的长函数。这样你就可以把长时间投给int:

<c:out value="${map[(1).intValue()]}"/>

#4


3  

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的值作为映射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>

#5


2  

If you just happen to have a Map with Integer keys you cannot change, you could write a custom EL function to convert a Long to Integer. This would allow you to do something like:

如果您刚好有一个带有整数键的映射,您无法更改,您可以编写一个定制的EL函数来转换一个长到整数。这会让你做一些事情,比如:

<c:out value="${map[myLib:longToInteger(1)]}"/>

#1


112  

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:

基本上自动装箱将一个整数对象放入映射中。即:

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,因此查找map中的键值为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.

作为一个Long,永远不等于一个整数对象,它没有在映射中找到条目。简单地说就是这样。


Update since May 2009 (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月,EL 2.2引入JSP 2.2 / Java EE 6,与EL 2.1相比有一些差异。看来(“EL表达式解析整数as long”):

you can call the method intValue on the Long object self inside EL 2.2:

可以在EL 2.2中调用长对象self的方法intValue:

<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>

#2


11  

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 hashmap中无法找到匹配。

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.

这将被视为一个长对象,然后有机会在包含映射映射或其他内容时匹配一个对象。

Then, continue as usual with something like

然后,像往常一样继续

${map[longKey]}

#3


7  

You can use all functions from Long, if you put the number into "(" ")". That way you can cast the long to an int:

如果你把数字填入“(”)”中,你就可以使用所有的长函数。这样你就可以把长时间投给int:

<c:out value="${map[(1).intValue()]}"/>

#4


3  

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的值作为映射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>

#5


2  

If you just happen to have a Map with Integer keys you cannot change, you could write a custom EL function to convert a Long to Integer. This would allow you to do something like:

如果您刚好有一个带有整数键的映射,您无法更改,您可以编写一个定制的EL函数来转换一个长到整数。这会让你做一些事情,比如:

<c:out value="${map[myLib:longToInteger(1)]}"/>