You can easily create a custom function to check for a property, according to a vivin blog post .
In short, if you already have your own taglib, it's just a matter of creating a static hasProperty method ...
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; } }
... and adding five lines to your 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>
... and calling it in your JSP
<c:if test="${myTld:hasProperty(myObject, 'myAttribute')}"> <c:set var="foo" value="${myObject.myAttribute}" /> </c:if>
sbk May 10 '12 at 9:41 a.m. 2012-05-10 09:41
source share