检查JSP中是否存在属性
我有一些扩展超类的类,我想在JSP中显示这些类的一些属性。我只想制作一个JSP,但我事先不知道对象是否有属性。因此,我需要一个JSTL表达式或一个标记来检查我传递的对象是否具有此属性(类似于javascript中的in运算符,但在服务器中)检查JSP中是否存在属性,jsp,jstl,jsp-tags,Jsp,Jstl,Jsp Tags,我有一些扩展超类的类,我想在JSP中显示这些类的一些属性。我只想制作一个JSP,但我事先不知道对象是否有属性。因此,我需要一个JSTL表达式或一个标记来检查我传递的对象是否具有此属性(类似于javascript中的in运算符,但在服务器中) ${myObject.myAttribute} 我怎么能得到这个 谢谢。你的意思是这样的: <c:if test="${not null myObject.myAttribute}"> <!-- Now I can access
${myObject.myAttribute}
我怎么能得到这个
谢谢。你的意思是这样的:
<c:if test="${not null myObject.myAttribute}">
<!-- Now I can access safely to "myAttribute" -->
</C:if>
或其他变体
<c:if test="${myObject.myAttribute != null}">
<!-- Now I can access safely to "myAttribute" -->
</C:if>
如果这是一个清单,你可以做
<c:if test="#{not empty myObject.myAttribute}">
使用JSTL
${myObject.myAttribute}
属性不可用。
您可以根据创建自定义函数来检查属性
简而言之,如果您已经有了自己的taglib,那么只需创建一个静态的“hasProperty”方法
import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.PropertyUtils;
...
public static boolean hasProperty(Object o, String propertyName) {
if (o == null || propertyName == null) {
return false;
}
try
{
return PropertyUtils.getPropertyDescriptor(o, propertyName) != null;
}
catch (Exception e)
{
return false;
}
}
…并在TLD中添加五行
<function>
<name>hasProperty</name>
<function-class>my.package.MyUtilClass</function-class>
<function-signature>boolean hasProperty(java.lang.Object,
java.lang.String)
</function-signature>
</function>
hasProperty
my.package.MyUtilClass
布尔属性(java.lang.Object,
(java.lang.String)
。。。并在JSP中调用它
<c:if test="${myTld:hasProperty(myObject, 'myAttribute')}">
<c:set var="foo" value="${myObject.myAttribute}" />
</c:if>
这是一个更详细的(典型的?)使用BalusC的好答案
<%--
[1] sets a default value for variable "currentAttribute"
[2] check if myObject is not null
[3] sets variable "currentAttribute" to the value of what it contains
[4] catches "property not found exception" if any
- if exception thrown, it does not output anything
- if not exception thrown, it outputs the value of myObject.myAttribute
--%>
<c:set var="currentAttribute" value="" /> <%-- [1] --%>
<c:if test="${not empty myObject}"> <%-- [2] --%>
<c:set var="currentAttribute"> <%-- [3] --%>
<c:catch var="exception">${myObject.myAttribute}</c:catch> <%-- [4] --%>
</c:set>
</c:if>
<%-- use the "currentAttribute" variable without worry in the rest of the code --%>
currentAttribute is now equal to: ${currentAttribute}
${myObject.myAttribute}
currentAttribute现在等于:${currentAttribute}
正如Shervin在BalusC回答的评论中指出的,这可能不是最干净的解决方案,但正如BalusC回答的那样,“这是迄今为止实现奇怪要求的唯一途径”
资源
<c:catch var="exception">
<c:if test="${object.class.getDeclaredField(field) ne null}">
</c:if>
</c:catch>
希望这有帮助。否,如果我执行myObject.myAttribute,而myObject没有myAttribute的getter,我将获得PropertyNotFoundException。一个对象有一个空值的属性和它没有这个属性是不同的。但是你应该如何访问这个属性呢?据我所知,它只能通过getter访问。即使变量是公共的,我相信您也需要一个getter。为什么你不能创建一个getter呢?这个属性并不存在于每个子类中,所以当这个属性存在时,我有一个getter。问题是我不知道要传递哪个子类。@Shervin我相信你的答案是“仅”检查属性是否设置(不是null,也不是空字符串),是我吗?我认为这是一种很难看的方式来判断变量是否存在。这就像在java中捕获NullPointerException而不是<代码>(如果!=null)@Shervin:这的确是一个糟糕的设计。但到目前为止,这是实现奇怪要求的唯一途径。@Shervin Asgari我认为“if null”很难看。除非您使用第三方api。您应该注意所有异常方式清理器。@ShervinAsgari-您是正确的,但是JSP/JSTL的情况通常是“漂亮”解决方案要么不可用,要么冗长,要么极其迂回(请参阅sbk的答案,它创建了一个完整的新标记库)。另一个很好的例子是尝试执行适当的
if/else if
块。如果要检查列表属性是否为null,请使用${list!=null}
,因为当列表为空列表时,${not empty list}
返回true。
<%--
[1] sets a default value for variable "currentAttribute"
[2] check if myObject is not null
[3] sets variable "currentAttribute" to the value of what it contains
[4] catches "property not found exception" if any
- if exception thrown, it does not output anything
- if not exception thrown, it outputs the value of myObject.myAttribute
--%>
<c:set var="currentAttribute" value="" /> <%-- [1] --%>
<c:if test="${not empty myObject}"> <%-- [2] --%>
<c:set var="currentAttribute"> <%-- [3] --%>
<c:catch var="exception">${myObject.myAttribute}</c:catch> <%-- [4] --%>
</c:set>
</c:if>
<%-- use the "currentAttribute" variable without worry in the rest of the code --%>
currentAttribute is now equal to: ${currentAttribute}
<c:catch var="exception">
<c:if test="${object.class.getDeclaredField(field) ne null}">
</c:if>
</c:catch>