tags:

views:

194

answers:

2

Hello,

I have some classes which extends a superclass, and in the JSP I want to show some attributes of these classes. I only want to make one JSP, but I don't know in advance if the object has an attribute or not. So I need a JSTL expression or a tag which checks that the object I pass has this attribute (similar to in operator in javascript, but in 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?

Thanks.

A: 

Do you mean something like this:

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

or other variant

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

If it is a list you can do

<c:if test="#{not empty myObject.myAttribute}">
Shervin
No, if I do myObject.myAttribute and myObject has not a getter for myAttribute I'll get a PropertyNotFoundException. It's not the same that an object has a property with null value that it doesn't have this property.
Javi
But how else are you supposed to access the property? It can only be access through getters as far as I know. Even if the variable is public I believe you need a getter.Why cant you just create a getter?
Shervin
The property is not present in every subclass, so when the property is present I have a getter for it. The problem is that I don't know which of the subclasses is going to be passed.
Javi
+1  A: 

Make use of JSTL c:catch.

<c:catch var="exception">${myObject.myAttribute}</c:catch>
<c:if test="${not empty exception}">Attribute not available.</c:if>
BalusC