25

I'm using h:outputText tags to display readonly data. Ex:

<h:outputText value="Phone Number:" />
<h:outputText value="#{userHandler.user.phoneNumber}" />

When "phoneNumber" is an empty string or a null, I want to display a dash "-" as the value.

Is there any easy way to do this maybe with expression language or something?

BTW, I thought about adding methods to the User class like getPhoneNumberDisplayText() that could do the check internally, but I since it's a view issue, I'd rather keep the code in the JSF page.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140

1 Answers1

55
<h:outputText value="#{userHandler.user.phoneNumber != null 
    ? userHandler.user.phoneNumber : '-'}" />

Or, you could make a new outputText:

<h:outputText rendered="#{userHandler.user.phoneNumber == null}" value="-" />
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 54
    Just use `empty`. Covers both `null` and empty string. `#{empty foo.property ? '-' : foo.property}` or other way, `#{not empty foo.property ? foo.property : '-'}`. – BalusC Jan 24 '11 at 23:34
  • @BalusC this doesn't work for `set` method `Illegal Syntax for Set Operation`. is there solution for that? – Darshana Jan 28 '14 at 04:41