Validation attribute exists in JSP

I have several classes that extend the superclass, and in JSP I want to show some attributes of these classes. I only want to make one JSP, but I do not know in advance if the object has an attribute or not. So I need a JSTL expression or a tag that verifies that the object I pass has this attribute (similar to a statement in javascript, but on the server).

<c:if test="${an expression which checks if myAttribute exists in myObject}"> <!-- Display this only when myObject has the atttribute "myAttribute" --> <!-- Now I can access safely to "myAttribute" --> ${myObject.myAttribute} </C:if> 

How can i get this?

Thank.

+38
jsp jstl jsp-tags
Mar 26 '10 at 10:41
source share
5 answers

Use JSTL c:catch .

 <c:catch var="exception">${myObject.myAttribute}</c:catch> <c:if test="${not empty exception}">Attribute not available.</c:if> 
+56
Mar 26
source share

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> 
+15
May 10 '12 at 9:41 a.m.
source share

You mean something like this:

 <c:if test="${not null myObject.myAttribute}"> <!-- Now I can access safely to "myAttribute" --> </C:if> 

or other option

 <c:if test="${myObject.myAttribute != null}"> <!-- Now I can access safely to "myAttribute" --> </C:if> 

If this is a list you can make

 <c:if test="#{not empty myObject.myAttribute}"> 
+2
Mar 26 '10 at 10:47
source share

Just a more verbose (typical?) Use of BalusC's excellent answer

 <%-- [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} 

As Sherwin noted in the comments on BalusC's answers, this is probably NOT the cleanest solution, but, as BalusC replied, "so far the only way to achieve an odd requirement."

Resources

+2
Oct. 01 '14 at 9:07
source share

The accepted answer may have some side effects when I just want to check if the object has a field, but do not want to display the value of the field. In the mentioned case, I use the snippet below:

  <c:catch var="exception"> <c:if test="${object.class.getDeclaredField(field) ne null}"> </c:if> </c:catch> 

I hope this helps.

+2
Sep 21 '16 at 3:30
source share



All Articles