Java 如果 jsf 数据表为空,如何显示消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1985718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:06:04  来源:igfitidea点击:

How do I display a message if a jsf datatable is empty?

javajsf

提问by blank

Using JSF1.2, if my datatable binding returns no rows I want to display a message saying so.

使用 JSF1.2,如果我的数据表绑定没有返回任何行,我想显示一条消息。

How do I do that?

我怎么做?

And for extra points - how do I hide the table completly if it's empty?

对于额外的积分 - 如果桌子是空的,我该如何完全隐藏它?

Thanks.

谢谢。

采纳答案by BalusC

Make use of the renderedattribute. It accepts a boolean expression. You can evaluate the datatable's value inside the expression with help of the EL's emptykeyword. If it returns false, the whole component (and its children) won't be rendered.

使用rendered属性。它接受一个布尔表达式。您可以在 ELempty关键字的帮助下评估表达式中数据表的值。如果它返回false,则不会呈现整个组件(及其子组件)。

<h:outputText value="Table is empty!" rendered="#{empty bean.list}" />

<h:dataTable value="#{bean.list}" rendered="#{not empty bean.list}">
    ...
</h:dataTable>

For the case you're interested, here are other basic examples how to make use of the EL powers inside the renderedattribute:

对于您感兴趣的情况,以下是如何在rendered属性中使用 EL 功能的其他基本示例:

<h:someComponent rendered="#{bean.booleanValue}" />
<h:someComponent rendered="#{bean.intValue gt 10}" />
<h:someComponent rendered="#{bean.objectValue eq null}" />
<h:someComponent rendered="#{bean.stringValue ne 'someValue'}" />
<h:someComponent rendered="#{not empty bean.collectionValue}" />
<h:someComponent rendered="#{not bean.booleanValue and bean.intValue ne 0}" />
<h:someComponent rendered="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />

See also:

也可以看看:

回答by Romain Linsolas

You can test this in several ways, for example by having a function in a bean that tests the list size:

您可以通过多种方式对此进行测试,例如通过在 bean 中使用一个函数来测试列表大小:

function boolean isEmpty() {
    return myList.isEmpty();
}

then in the JSF pages :

然后在 JSF 页面中:

<h:outputText value="List is empty" rendered="#{myBean.empty}"/>

<h:datatable ... rendered="#{!myBean.empty}">
...
</h:datatable>