JSF:h:outputText;值是空字符串时如何显示破折号? [英] JSF: h:outputText; how to show a dash when the value is empty string?

查看:126
本文介绍了JSF:h:outputText;值是空字符串时如何显示破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用h:outputText标记来显示只读数据.例如:

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

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

当"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?

顺便说一句,我曾考虑过将诸如getPhoneNumberDisplayText()之类的方法添加到User类中,从而可以在内部进行检查,但是由于是视图问题,我宁愿将代码保留在JSF页面中.

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 : '-'}" />

或者,您可以创建一个新的outputText:

Or, you could make a new outputText:

<h:outputText rendered="#{userHandler.user.phoneNumber == null}" value="-" />

这篇关于JSF:h:outputText;值是空字符串时如何显示破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆