java 检查JSTL中是否存在变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7023375/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:19:42  来源:igfitidea点击:

Check if variable exists in JSTL

javajspjstl

提问by Hedge

I've got a PersistenceSet and would like to check if it contains a certain variable.

我有一个 PersistenceSet 并且想检查它是否包含某个变量。

How can I check in the JSTL whether subitem exists or not?

如何在 JSTL 中检查子项是否存在?

However when I try to access a non-existant variable like this:

但是,当我尝试访问这样一个不存在的变量时:

<c:if test="${not empty item.subitem}">       
   <c:out value="${item.subitem}" /><br />
</c:if>

I get a PropertyNotFoundException:

我得到一个 PropertyNotFoundException:

Property 'subitem' not found on type com.company.classname

在 com.company.classname 类型上找不到属性“subitem”

回答by Jigar Joshi

It is clear that standard setter/getter is not available for subitemin the class

很明显,subitem类中没有标准的 setter/getter

If you want to check if the property is available for the class you can go for following tweak

如果您想检查该属性是否可用于该类,您可以进行以下调整

using c:catch

使用 c:catch

<c:catch var="exception">${item.subitem}</c:catch>
<c:if test="${exception==null}">subitemnot available.</c:if>