Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Initial answer (EL 2.1, May 2009) </p> <p>As mentioned in <a href="http://forums.sun.com/thread.jspa?messageID=9702932#9702932" rel="noreferrer">this java forum thread</a>:</p> <blockquote> <p>Basically autoboxing puts an Integer object into the Map. ie:</p> </blockquote> <pre><code>map.put(new Integer(0), "myValue") </code></pre> <p>EL (Expressions Languages) evaluates 0 as a Long and thus goes looking for a Long as the key in the map. ie it evaluates: </p> <pre><code>map.get(new Long(0)) </code></pre> <p>As a <code>Long</code> is never equal to an <code>Integer</code> object, it does not find the entry in the map.<br> That's it in a nutshell.</p> <hr> <h2>Update since May 2009 (EL 2.2)</h2> <p><a href="https://stackoverflow.com/a/4812883/6309">Dec 2009 saw the introduction of EL 2.2 with JSP 2.2 / Java EE 6</a>, with a <a href="https://stackoverflow.com/a/7209200/6309">few differences compared to EL 2.1</a>.<br> It seems ("<a href="https://stackoverflow.com/a/18269163/6309">EL Expression parsing integer as long</a>") that:</p> <blockquote> <p><strong>you can call the method <code>intValue</code> on the <code>Long</code> object self inside EL 2.2</strong>:</p> </blockquote> <pre><code>&lt;c:out value="${map[(1).intValue()]}"/&gt; </code></pre> <p>That could be a good workaround here (also mentioned below in <a href="https://stackoverflow.com/users/4371659/tobias-liefke">Tobias Liefke</a>'s <a href="https://stackoverflow.com/a/27533414/6309">answer</a>)</p> <hr> <p>Original answer:</p> <p>EL uses the following wrappers:</p> <pre><code>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 </code></pre> <p>JSP page demonstrating this:</p> <pre><code> &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; &lt;%@ page import="java.util.*" %&gt; &lt;h2&gt; Server Info&lt;/h2&gt; Server info = &lt;%= application.getServerInfo() %&gt; &lt;br&gt; Servlet engine version = &lt;%= application.getMajorVersion() %&gt;.&lt;%= application.getMinorVersion() %&gt;&lt;br&gt; Java version = &lt;%= System.getProperty("java.vm.version") %&gt;&lt;br&gt; &lt;% 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); %&gt; &lt;h3&gt;Looking up map in JSTL - integer vs long &lt;/h3&gt; 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. &lt;table border="1"&gt; &lt;tr&gt;&lt;th&gt;Key&lt;/th&gt;&lt;th&gt;value&lt;/th&gt;&lt;th&gt;Key Class&lt;/th&gt;&lt;/tr&gt; &lt;c:forEach var="entry" items="${myMap}" varStatus="status"&gt; &lt;tr&gt; &lt;td&gt;${entry.key}&lt;/td&gt; &lt;td&gt;${entry.value}&lt;/td&gt; &lt;td&gt;${entry.key.class}&lt;/td&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; &lt;h4&gt; Accessing the map&lt;/h4&gt; Evaluating: ${"${myMap['2']}"} = &lt;c:out value="${myMap['2']}"/&gt;&lt;br&gt; Evaluating: ${"${myMap[2]}"} = &lt;c:out value="${myMap[2]}"/&gt;&lt;br&gt; Evaluating: ${"${myMap[42]}"} = &lt;c:out value="${myMap[42]}"/&gt;&lt;br&gt; &lt;p&gt; 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 &lt;p&gt; lifeInteger = &lt;%= lifeInteger %&gt;&lt;br/&gt; lifeLong = &lt;%= lifeLong %&gt;&lt;br/&gt; lifeInteger.equals(lifeLong) : &lt;%= lifeInteger.equals(lifeLong) %&gt; &lt;br&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload