Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems that the only way of getting the value for the <code>null</code> key using standard EL implementation is to <strong>call <code>get()</code> method on the map</strong> (considering that you said <code>keyObject.keyProp</code> resolves to <em>Integer</em> object):</p> <pre><code>&lt;c:out value="${map.get(keyObject.keyProp)}" /&gt; </code></pre> <p>I tested this solution and it works.</p> <p>Actually, in this case, you can easily do without <code>&lt;c:out /&gt;</code>, just use plain EL where you need it, e.g.</p> <pre><code>${map.get(keyObject.keyProp)} </code></pre> <hr> <h2>Simple example:</h2> <p><em>TestMapServlet.java</em></p> <pre><code>package com.example; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.util.Map; import java.util.HashMap; import java.io.IOException; public class TestMapServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map&lt;Integer, Object&gt; map = new HashMap&lt;Integer, Object&gt;(); Integer noValueInt = null; Integer one = new Integer(1); Integer two = new Integer(2); map.put(noValueInt, "Default object for null Integer key"); map.put(one, "Object for key = Integer(1)"); map.put(two, "Object for key = Integer(2)"); request.setAttribute("map", map); request.setAttribute("noValueInt", noValueInt); request.setAttribute("one", one); request.setAttribute("two", two); request.getRequestDispatcher("/test-map.jsp").forward(request, response); } } </code></pre> <p><br /> <em>test-map.jsp</em></p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Home Page&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Testing access to java.util.Map using just EL&lt;/h1&gt; &lt;p&gt;&lt;b&gt;\${map.get(noValueInt)}&lt;/b&gt;: ${map.get(noValueInt)}&lt;/p&gt; &lt;p&gt;&lt;b&gt;\${map[one]}&lt;/b&gt;: ${map[one]}&lt;/p&gt; &lt;p&gt;&lt;b&gt;\${map[two]}&lt;/b&gt;: ${map[two]}&lt;/p&gt; &lt;h1&gt;Testing access to java.util.Map using JSTL and EL&lt;/h1&gt; &lt;p&gt;&lt;b&gt;&amp;lt;c:out value="\${map.get(noValueInt)}" /&amp;gt; &lt;/b&gt;: &lt;c:out value="${map.get(noValueInt)}" /&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;&amp;lt;c:out value="\${map[one]}" /&amp;gt; &lt;/b&gt;: &lt;c:out value="${map[one]}" /&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;&amp;lt;c:out value="\${map[two]}" /&amp;gt; &lt;/b&gt;: &lt;c:out value="${map[two]}" /&gt;&lt;/p&gt; &lt;h2&gt;Printing java.util.Map keys and values (when Key = null, the &lt;i&gt;null&lt;/i&gt; won't be shown)&lt;/h2&gt; &lt;c:forEach items="${map}" var="entry"&gt; &lt;p&gt;&lt;b&gt;Key&lt;/b&gt; = "${entry.key}", &lt;b&gt;Value&lt;/b&gt; = "${entry.value}"&lt;/p&gt; &lt;/c:forEach&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><br /> <em>web.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt; &lt;servlet&gt; &lt;servlet-name&gt;Test Map Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;com.example.TestMapServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Test Map Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/TestMap.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p><br /></p> <p><strong>IMPORTANT NOTE</strong><br> To be able to invoke methods with arguments using EL you must use minimum <em>Servlet version 3.0</em>.<br> Quote from here: <a href="https://stackoverflow.com/tags/el/info">https://stackoverflow.com/tags/el/info</a> </p> <blockquote> <p>Since EL 2.2, which is maintained as part of Servlet 3.0 / JSP 2.2 (Tomcat 7, Glassfish 3, JBoss AS 6, etc), it's possible to invoke non-getter methods, if necessary with arguments.</p> </blockquote> <hr> <p>Apart from the above solution you could use <strong>custom Unified Expression Language implementation</strong> such as <a href="http://juel.sourceforge.net/index.html" rel="nofollow noreferrer"><strong>JUEL</strong></a> that has an alternative solution. </p> <p>An <strong>explanation why</strong> it is not possible (in the standard implementation) to access <em>map</em> value by the <code>null</code> <em>key</em> using <code>[]</code> and the custom solution can be found in <a href="http://juel.sourceforge.net/juel.pdf" rel="nofollow noreferrer"><strong>Java Unified Expression Language (JUEL) documentation</strong></a> (emphasis in paragraphs is mine): </p> <blockquote> <p><h2><strong>2.5. Advanced Topics</strong></h2> ... <h3>Enabling/Disabling null Properties</h3> The EL specification describes the evaluation semantics of <code>base[property]</code>. If property is null, the specification states not to resolve null on base. Rather, null should be returned if <code>getValue(...)</code> has been called and a <code>PropertyNotFoundException</code> should be thrown else. As a consequence, it is impossible to resolve null as a key in a map. However, <strong>JUEL's expression factory may be configured to resolve null like any other property value</strong>. To enable (disable) null as an EL property value, you may set property <code>javax.el.nullProperties</code> to true (false). </p> <p>Assume that identifier map resolves to a <code>java.util.Map</code>. </p> <ul> <li><p>If feature <code>javax.el.nullProperties</code> has been disabled, evaluating <code>${base[null]}</code> as an rvalue (lvalue) will return null (throw an exception). </p></li> <li><p><strong>If feature <code>javax.el.nullProperties</code> has been enabled</strong>, evaluating <code>${base[null]}</code> as an rvalue (lvalue) will get (put) the value for key null in that map. The default is not to allow null as an EL property value. </p></li> </ul> <p>...</p> </blockquote> <p>Hope this will help.</p>
    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