Search code examples
nullhashmapjstl

How do I set this map value to null in JSTL?


In JSTL, I have HashMap as a page variable. The HashMap maps Strings to more complex Objects. I would like to set a value to null. So I'm trying

value: ${myMap[myId]} 
<c:set target="myMap" property="${myId}" value="${null}" />

I have verified taht the "value:" statement does print something out, however, I get the following JSTL error

javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  "51b21a0410340adf6501db08"

Any ideas how to set the particular entry in my map to null?


Solution

  • You can just do:

    value: ${myMap[myId]} 
    <c:set target="myMap" property="${myId}" value="" />
    

    and then in later code you can test if it's empty with:

    <c:if test="${myMap[myId] is empty}">
    

    and avoid trying to deal with "null" in JSTL.

    The (probably) better option is to remove it from the map entirely:

    <c:set var="debug" value="${myMap.remove(myId)}"/>
    

    Then your empty check will still work and you won't have to tote around null entries in your hashmap.