java JSTL 中的哈希映射键检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25177487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hash Map key check in JSTL
提问by Amz
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)
需要帮助。我有一个从 spring 控制器返回到 JSP 的哈希映射。有没有办法只检查某个键是否存在而不管任何值(该值也可能为空)
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.
如果键 REGION 存在(值可以是任何内容,包括空值),则在 jsp 中显示某些部分。
I am trying to access the key as ${hmap['REGION']}
我正在尝试以 ${hmap['REGION']} 的身份访问密钥
Thanks in advance.
提前致谢。
回答by Vipul Paralikar
try using containsKey:
尝试使用 containsKey:
${hmap.containsKey('REGION')}
回答by user3145373 ツ
<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.
检查这个,这个解决方案是否有效?并在不工作时发布并发布您的 JSP 的 jstl 代码。
回答by Brett Ryan
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.
我知道您想测试值可能包含空值的位置,但是有一种解决方案是使用empty
优雅的运算符,当人们可能还想排除空值时,它会评估true
值是否为空值或不存在.
<c:if test="${not empty hmap['REGION']}">
<%-- conditional block --%>
</c:if>