EL通过Integer键访问映射值 [英] EL access a map value by Integer key

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

问题描述

我有一个由Integer键入的Map。使用EL,如何通过键访问值?

I have a Map keyed by Integer. Using EL, how can I access a value by its key?

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

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

跟进:我追查了问题。显然 $ {name [1]} 使用数字作为 Long 进行地图查找。当我将 HashMap 更改为 TreeMap 并收到错误时,我想到了这一点:

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:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

如果我将地图更改为:

Map<Long, String> map = new HashMap<Long, String>();
map.put(1L, "One");

然后 $ {name [1]} 返回一。那是什么?为什么< c:out> 将数字视为long。对我来说似乎违反直觉(因为int更常用而不是长)。

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

所以我的新问题是,是否有一个EL表示法通过<$ c来访问地图$ c>整数价值?

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

推荐答案

初步答复(EL 2.1,2009年5月)

Initial answer (EL 2.1, May 2009)

此java论坛帖子中所述


基本上,自动装箱会将一个Integer对象放入Map中。
ie:

Basically autoboxing puts an Integer object into the Map. ie:



map.put(new Integer(0), "myValue")

EL(表达式语言)将0评估为Long,因此寻找Long作为键入地图。
即它评估:

EL (Expressions Languages) evaluates 0 as a Long and thus goes looking for a Long as the key in the map. ie it evaluates:

map.get(new Long(0))

作为永远不等于整数 object,它在地图中找不到该条目。

简而言之。

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.

2009年12月推出了带有JSP 2.2 / Java EE 6的EL 2.2 ,其中与EL 2.1相比存在一些差异

似乎( EL表达式解析整数很长):( / p>

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:


你可以在上调用方法 intValue EL 2.2内的长对象自我

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



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

这可能是一个很好的解决方法(下面也在Tobias Liefke 回答

That could be a good workaround here (also mentioned below in Tobias Liefke's answer)

原始答案:

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页面展示了这一点:

JSP page demonstrating this:

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

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

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