Hash Map key check in JSTL

你说的曾经没有我的故事 提交于 2021-02-07 14:21:43

问题


Need help.I have a hash map which is returned from a spring controller to JSP.Is there a way just to check if a certain key exists irrespective of any value(the value may be null too)

Say, the below hash map is being sent from the controller

HashMap hmap = new HashMap();
hmap.put("COUNTRY", "X");
hmap.put("REGION", null);

If the key REGION exists ( value may be anything including null) then display some section in the jsp.

I am trying to access the key as ${hmap['REGION']}

Thanks in advance.


回答1:


try using containsKey:

${hmap.containsKey('REGION')}



回答2:


<c:forEach var="entry" items="${hmap }" varStatus="status">        
      <c:if test="${entry.key == 'REGION'}">
        <tr>
           <td>${entry.key}</td>
           <td>${entry.value}</td>
        </tr>
      </c:if>
</c:forEach>

Check this, if this solution works or not ? And post if not working and also post your JSP's jstl code.




回答3:


I know that you want to test for where the value may include null, but there's a solution by using the empty operator that is elegant when one might want to also exclude null values in that it will evaluate to true if the value is null or does not exist.

<c:if test="${not empty hmap['REGION']}">
    <%-- conditional block --%>
</c:if>


来源:https://stackoverflow.com/questions/25177487/hash-map-key-check-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!