Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF and expression language: Bind property only when it exists

Tags:

java

jsf

el

what is the best way to bind a datacolumn to a property that might or might not exist in the datasource?

This happens for example when you have a class hierarchy where some children might be of a subtype which has the property. The datasource contains various subclass types.

<DataColumn outputText="#{item.property}" />

always yields a PropertyNotFoundException when the property isn't present in one of the subclasses. I don't want to include the property in the base class because it shouldn't be there according to the business rules.

How would you solve this problem?

like image 421
Falcon Avatar asked Apr 11 '11 09:04

Falcon


1 Answers

Without changing the classes, your best bet is to do kind of an instanceof in EL. You can do that by checking the (simple) classname as obtained by Object#getClass() and then Class#getName() or Class#getSimpleName() in EL.

Assuming that the class with the property has the full qualified name com.example.SubItem, here's an example:

<h:outputText value="#{item.property}" rendered="#{item.class.name == 'com.example.SubItem'}" />

or

<h:outputText value="#{item.property}" rendered="#{item.class.simpleName == 'SubItem'}" />
like image 100
BalusC Avatar answered Nov 09 '22 07:11

BalusC