【问题标题】:EL access a map value by Integer keyEL 通过整数键访问映射值
【发布时间】:2010-10-29 19:05:08
【问题描述】:

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

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");

我认为这会起作用,但它不起作用(地图已经在请求的属性中):

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

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

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]} 返回“一”。那是怎么回事?为什么&lt;c:out&gt; 将数字视为 long。对我来说似乎违反直觉(因为 int 比 long 更常用)。

所以我的新问题是,是否有 EL 表示法可以通过 Integer 值访问地图?

【问题讨论】:

    标签: java jsp jstl el


    【解决方案1】:

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

    如this java forum thread中所述:

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

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

    EL(表达式语言)将 0 评估为 Long,因此会寻找 Long 作为映射中的键。 即它评估:

    map.get(new Long(0))
    

    由于Long 永远不等于Integer 对象,因此它在映射中找不到条目。
    简而言之就是这样。


    自 2009 年 5 月起更新 (EL 2.2)

    Dec 2009 saw the introduction of EL 2.2 with JSP 2.2 / Java EE 6,带有few differences compared to EL 2.1。
    看来(“EL Expression parsing integer as long”):

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

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

    这可能是一个很好的解决方法(在下面Tobias Liefke 的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 页面演示了这一点:

     <%@ 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 将数字扩展为整数?
    • @Steve:确实,EL 似乎不支持这一点。
    • 我从谷歌搜索中找到了这个问题和答案。果然,当我从 Map 切换到 Map 时,我就能够使用我在 JSP 页面中的 EL 从中提取。谢谢!!
    • @Steve:有可能——看我的回答
    • @SteveKuo 确实应该有可能。我现在意识到这个 6 年前的答案是在 EL 2.2 尚未发布时编写的。我已经编辑并更新了上述答案。
    【解决方案2】:

    除了上述注释之外,另一个有用的提示是当您在某个变量(例如请求参数)中包含一个字符串值时。 在这种情况下,将其传入也会导致 JSTL 将“1”的值作为字符串键入,因此在 Map hashmap 中找不到匹配项。

    解决这个问题的一种方法是做这样的事情。

    <c:set var="longKey" value="${param.selectedIndex + 0}"/>
    

    现在这将被视为一个 Long 对象,然后当它包含在地图 Map 或其他内容中时,有机会匹配一个对象。

    然后,像往常一样继续

    ${map[longKey]}
    

    【讨论】:

      【解决方案3】:

      如果将数字放入“(”“)”中,则可以使用 Long 中的所有功能。这样您就可以将 long 转换为 int:

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

      【讨论】:

      • 我没有立即看到您的回答。 +1。为了提高知名度,我已在答案中包含并记录了这种可能性。
      【解决方案4】:

      根据上面的帖子,我尝试了这个,效果很好 我想使用 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>
      

      【讨论】:

        【解决方案5】:

        如果您碰巧有一个 Map 和 Integer 无法更改的键,您可以编写一个 custom EL function 将 Long 转换为 Integer。这将允许您执行以下操作:

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

        【讨论】:

          猜你喜欢
          • 2013-06-13
          • 1970-01-01
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多