How to output a string attribute of an object that may be null

As part of the dataTable on the seam JSF page, for one column, the name output is required:

<h:outputText value="#{listing.staffMember.name}"/>

The problem is that "staffMember" may be empty for some lists, so I get an error:

javax.el.ELException: /xxxxx.xhtml @42,67 value="#{listing.staffMember.name}": Error reading 'name' on type xxxx.model.AgentStaff_$$_javassist_152

If the value is null, I do not want any text to be displayed. I tried this:

<h:outputText value="#{listing.staffMember.name}" rendered="#{listing.staffMember != null}"/>

But the same error occurs.

How can I infer a property of an object that may be null?

+3
source share
2 answers

Perhaps you can use the ternary operator , which will look something like this:

value="#{listing.staffMember != null ? listing.staffMember.name : 'None'}"

Or you can use c: if tag .

+5

( ):

<h:outputText value="#{listing.staffMember.name}" 
              rendered="#{not empty listing.staffMember}"/>

, .

+3

Source: https://habr.com/ru/post/1704048/


All Articles