开发者

JSF: h:outputText; how to show a dash when the value is empty string?

开发者 https://www.devze.com 2023-02-07 00:45 出处:网络
I\'m using h:outputText tags to display readonly data.Ex: <h:outputText value=\"Phone Number:\" />

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 开发者_Python百科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.


<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="-" />
0

精彩评论

暂无评论...
验证码 换一张
取 消