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?
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'}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With