I have a Map<Integer, Object>
in passed to JSP from a controller. There is a null key with a default value, that means map.get(null)
returns a default object. keyObject.keyProp
is Integer and might be null.
When I use this in jsp
<c:out value="${map[keyObject.keyProp]}"/>
I do not get any output for the null keys. Is there any way to make null keys work in jsp?
Best Answer
It seems that the only way of getting the value for the
null
key using standard EL implementation is to callget()
method on the map (considering that you saidkeyObject.keyProp
resolves to Integer object):I tested this solution and it works.
Actually, in this case, you can easily do without
<c:out />
, just use plain EL where you need it, e.g.Simple example:
TestMapServlet.java
test-map.jsp
web.xml
IMPORTANT NOTE
To be able to invoke methods with arguments using EL you must use minimum Servlet version 3.0.
Quote from here: https://stackoverflow.com/tags/el/info
Apart from the above solution you could use custom Unified Expression Language implementation such as JUEL that has an alternative solution.
An explanation why it is not possible (in the standard implementation) to access map value by the
null
key using[]
and the custom solution can be found in Java Unified Expression Language (JUEL) documentation (emphasis in paragraphs is mine):Hope this will help.